python正则之match search findall

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']
posted @ 2018-03-09 13:23  定静沉行  阅读(299)  评论(0编辑  收藏  举报