Regular Expressions in Python
The comprehension of Regular Expressions in Python mainly refers to the understanding of symbols. The main symbols are:
. One character is matched by default and does not contain line breaks. If DOTALL is set, it matches the line break ^ matches the first of the line $ matches the end of the line * matches 0 or multiple duplicates + matches one or more duplicates? Match one or zero duplicates *?, + ?,?? Match {m}, {m, n}, {m, n} according to non-Greedy mode }? Match m duplicates, m to n duplicates, and m to n duplicates according to non-Greedy mode \ escape [] [abc], [a-z] [^ a-z] | or match 'a | B '(...) matching group (? ILmsux)
(? :...)(? P ...) >>> Re. match ('(? P Abc) {2} ', 'abcabc'). groupdict () {'name': 'abc '}
(? P = name) >>> re. match (R '(? P Abc )----(? P = name) ', 'abc ---- abc'). group ()
'Abc ---- abc'
(? #...) # The following content is a comment
(? = ...)
The content following the matched string needs to be matched.
>>> Re. match (r'phone (? = \ D {3}) ', 'phone123'). group ()
'Phone '#
(?!...)
The matched characters cannot match
>>> Re. match (r'phone (?! \ D {3}) ', 'phoneabc123'). group ()
'Phone'
(? <= ...)
Match before the matched string
>>> Re. search ('(? <= \ D) def ', '1def'). group ()
'Def'
(? > Re. search ('(? 'Def'
(? (Id/name) yes-pattern | no-pattern) >>> re. search ('(\ d) def (? (1)-abc |-123) ', '0def-abc'). group ()
'0def-abc'
\ Number
\ A matches the start of A string
\ B match word boundary
\ B \ B's antsense \ d [0-9]
\ D [^ 0-9]
\ S [\ t \ r \ n \ f \ v]
\ S non-blank characters
\ W [a-zA-Z0-9]
\ W \ w antsense
\ Z: End of matching string