python-re使用举例

 

 

代码:

 1 import re
 2 
 3 text = "JGood is a handsome boy, he is cool, clever, and so on..."
 4 print(text)
 5 m = re.match(r"(\w+)\s", text)
 6 n = re.search(r"\shan(ds)ome\s", text)
 7 if m:
 8     print("match: \"(\w+)\s\" ")
 9     print(m.group(0))
10 else:
11     print 'not match'  
12 
13 if n:
14     print("search: \"\shan(ds)ome\s\"")
15     print(n.group(0))
16 else:
17     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

posted @ 2016-07-22 14:52  月色深潭  阅读(325)  评论(0编辑  收藏  举报