Compile compiles faster, FindAll returns a matching list
>>> Import re
>>> r=r ' a[bcd]e '
>>> P=re.compile (R)
>>> print P.findall (' Abe,ace,ade,afe ')
[' Abe ', ' Ace ', ' Ade ']
>>> Print Re.findall (R, ' Abe,ace,ade,afe ')
[' Abe ', ' Ace ', ' Ade ']
>>>
Finditer returns a matching iterator
>>> Print Re.finditer (R, ' Abe,ace,ade,afe ')
<callable-iterator Object at 0x02c5ca90>
The match matches the string beginning with the first matched content returned
>>> r=r ' (a[bcd]e) ' #注意使用match和search时需要分组
>>> Res=re.match (R, ' Abe,ace,ade,afe ')
>>> Print res.groups ()
(' Abe ',)
>>> Res=re.match (R, ' Afe,abe,ace,ade ')
>>> Print Res
None
>>>
Search matches string full-text and returns the first matching content
>>> Res=re.search (R, ' Afe,abe,ace,ade ')
>>> Print Res
<_sre. Sre_match Object at 0x02c5ee60>
>>> Print res.groups ()
(' Abe ',)
>>>
Sub string regular substitution, return replacement string
>>> re.sub (R ' a). d ', ' python ', ' ABCD,AEFDQ,QANMD ')
' Python,pythonq,qpython '
>>>
Subn string Regular substitution, returns a tuple (replacement string, number of replacements)
>>> re.subn (R ' a). d ', ' python ', ' ABCD,AEFDQ,QANMD ')
(' Python,pythonq,qpython ', 3)
>>>
Split returns the list after cut
>>> Re.split (R ' \+ ', "12+34*56/78")
[' 12 ', ' 34*56/78 ']
>>> Re.split (R ' [\+\-\*\/] ', "12+34*56/78")
[' 12 ', ' 34 ', ' 56 ', ' 78 ']
>>>
This article from "Today's efforts, tomorrow's success!" "Blog, be sure to keep this provenance http://zhzhgo.blog.51cto.com/10497096/1676204
Python Regular common functions