python regular expression basic grammar

First, the most commonly used matching syntax

re.match scratch match

re.search match included

re.findall all the characters into the match to return a list of elements

re.split as to match the character list separator

re.sub matching characters and replace

 

Second, the common regular expression notation

'.'       

In addition to the default match any character other than \ n, if the specified flag DOTALL, it matches any character, including newline

'^'        

 Matches the beginning of the character, if the designated flags MULTILINE, which may be matched (r "^ a", " \ nabc \ neee", flags = re.MULTILINE)

'$'        

 End of the match character, or e.search ( "foo $", " bfoo \ nsdfsf", flags = re.MULTILINE) .group ()

'*'         

Matching character * number of 0 or more times before, re.findall ( "ab *", "cabb3abcbbac") results [ 'abb', 'ab' , 'a']

'+'         

Character before a match one or more times, re.findall ( "ab +", "ab + cd + abb + bba") Results [ 'ab', 'abb' ]

'?'         

Matches the preceding character 1 or 0 times

'{m}'      

Matches the preceding character m times

'{n,m}'   

Before a matching character n to m times, re.findall ( "ab {1,3} ", "abb abc abbcbbb") Results 'abb', 'ab', 'abb']

'|'         

Matching | left or | the right character, re.search. ( "Abc | ABC ", "ABCBabcCD") group () results 'ABC'

'(...)'   

Packet matches, the re.search ( "(ABC) {2} A (123 | 456) C", "abcabca456c"). Group () results abcabca456c

'[]'            

Character set, match all characters in brackets

       

'\ Z'     end of the match character, the same $

'\ d' matching digits 0-9    

'\ D' matches the non-digital    

'\ w' animals distribution [A-Za-z0-9]    

'\ W'     matches non [A-Za-z0-9]

'\ s' match blank characters, \ T, \ n-, \ R & lt, the re.search ( "\ + S", "ab & \ TC1 \ N3"). Group () Results '\ t'     

 

Guess you like

Origin www.cnblogs.com/Chamberlain/p/12369971.html