Regular Expressions in Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.