python-re使用举例
代码:
import re text = "JGood is a handsome boy, he is cool, clever, and so on..."
print(text)
m = re.match(r"(\w+)\s", text)
n = re.search(r"\shan(ds)ome\s", text)
if m:
print("match: \"(\w+)\s\" ")
print(m.group(0))
else:
print 'not match' if n:
print("search: \"\shan(ds)ome\s\"")
print(n.group(0))
else:
print 'not match'
执行结果:
JGood is a handsome boy, he is cool, clever, and so on...
match: "(\w+)\s"
JGood
search: "\shan(ds)ome\s"
handsome
参考:
http://www.cnblogs.com/sevenyuan/archive/2010/12/06/1898075.html
正则表达式参考:
http://www.runoob.com/regexp/regexp-syntax.html
https://msdn.microsoft.com/zh-cn/library/ae5bf541(v=vs.80).aspx
python-re使用举例的更多相关文章
- python yield用法举例说明
1 yield基本用法 典型的例子: 斐波那契(Fibonacci)數列是一个非常简单的递归数列,除第一个和第二个数外,任意一个数都可由前两个数相加得到.1 2 3 5 8…… def fab(ma ...
- 平时代码中不符合python风格的举例
良好的代码风格体现出自己的专业,良好的代码风格,方便同事之间协作. 下面举例讲讲自己在代码中不符合python风格的一些情况,方便自己写出优雅的代码. 段落之间空两行 很长的参数用括号连接但是不要超过 ...
- Python装饰器举例分析
概述 装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象. 我们要需要一个能测试函数运行时间的decorator,可以定义如 ...
- python之多线程举例
# 多线程举例 from threading import Thread from threading import current_thread class messager(Thread): de ...
- python with用法举例
我们知道在操作文件对象的时候可以这么写 with open('a.txt') as f: '代码块' 上述叫做上下文管理协议,即with语句,为了让一个对象兼容with语句,必须在这个对象的类中声明_ ...
- python字符串调用举例
以如下打印为例: my name is tom and my age is 12 方式一:字符串格式化表达式 name = 'tom' age = 12 print("my name is ...
- python之工作举例:通过复制NC文件来造数据
# 通过对NC文件复制来造数据 import os, shutil # 遍历的根目录 root_dir = "D:\\test_data\\DISASTER\\" # 获取NC文件 ...
- Python进程池举例
multiprocessing包 from multiprocessing import Pool def a(num): print num if __name__ == "__main_ ...
- 常用正则表达式最强汇总(含Python代码举例讲解+爬虫实战)
大家好,我是辰哥~ 本文带大家学习正则表达式,并通过python代码举例讲解常用的正则表达式 最后实战爬取小说网页:重点在于爬取的网页通过正则表达式进行解析. 正则表达式语法 Python的re模块( ...
- raw_input() 与 input() __ Python
这两个均是 python 的内建函数,通过读取控制台的输入与用户实现交互.但他们的功能不尽相同.举两个小例子. 1 >>> raw_input_A = raw_input(" ...
随机推荐
- jave 金额科学记数法处理
金额 :amount amount.stripTrailingZeros().toPlainString();
- 【BZOJ3218】a + b Problem 可持久化线段树优化建图
[BZOJ3218]a + b Problem 题解:思路很简单,直接最小割.S->i,容量为Bi:i->T,容量为Wi:所有符合条件的j->new,容量inf:new->i, ...
- poj 2154 Color < 组合数学+数论>
链接:http://poj.org/problem?id=2154 题意:给出两个整数 N 和 P,表示 N 个珠子,N种颜色,要求不同的项链数, 结果 %p ~ 思路: 利用polya定理解~定理内 ...
- java编程之JDBC
JDBC的常用类和接口 1. DriverManager类 管理数据库中的所有驱动程序,其所有的方法都是静态方法,调用时无需实例化,通过类名就可以直接调用. 2. Connec ...
- Rethinking the Inception Architecture for Computer Vision
https://arxiv.org/abs/1512.00567 Convolutional networks are at the core of most state-of-the-art com ...
- appium(1)-about appium
about appium Introduction to Appium Appium is an open-source tool for automating native, mobile web, ...
- HBase开发
MapReduce接口 HBase提供了TableInputFormat.TableOutputFormat.TableMapper和TableReducer类来支持使用MapReduce框架处理HB ...
- jvm调试
https://www.usenix.org/legacy/events/jvm01/full_papers/russell/russell_html/index.html
- CSS阶段总结
CSS布局之左右布局与左中右布局 方法:为子元素设置浮动,然后在其父元素上使用clearfix类来清除浮动.代码示例: html部分: <div class="parent clear ...
- Appium——Error while obtaining UI hierarchy XML file:com.android.ddmlib.SyncException:
使用uiautomatorviewer查看页面元素时报这个错误,解决办法 cmd: adb root ok 解决