码迷,mamicode.com
首页 > 编程语言 > 详细

python正则之match search findall

时间:2018-03-09 14:10:31      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:lin   python   strong   pytho   nbsp   post   串匹配   search   gpo   

match:只匹配一次,开头匹配不上,则不继续匹配 a,b,\w+
match(a,"abcdef") 匹配a
>>> re.match("a","abcdef").group()
‘a‘
match(b,"abcdef")
>>> print re.match("b","abcdef")
None
match("\w+","abcdef")
search ,全字符串匹配,但是只匹配一次,匹配不上则不继续匹配 a ,b,\w+
>>> re.search("a","abcdef abc 123 456").group()
‘a‘
>>> re.search("b","abcdef abc 123 456").group()
‘b‘
 
>>> re.search("\w+","abcdef abc 123 456").group()
‘abcdef‘
 
findall
注:findall 返回列表 ,列表不能group()
 
>>> print re.findall(r"b","abcdef abc 123 456")
[‘b‘, ‘b‘]
 
>>> print re.findall(r"a","abcdef abc 123 456")
[‘a‘, ‘a‘]
 
>>> print re.findall(r"\w+","abcdef abc 123 456")
[‘abcdef‘, ‘abc‘, ‘123‘, ‘456‘]

python正则之match search findall

标签:lin   python   strong   pytho   nbsp   post   串匹配   search   gpo   

原文地址:https://www.cnblogs.com/zyy98877/p/8533387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!