Analysis of Common Python Regular Expression symbols

Source: Internet
Author: User
This article mainly introduces Python's common Regular Expression symbols, which are very practical. If you need them, you can refer to the comprehension of Regular Expressions in Python, mainly understanding the symbols, this article analyzes the regular expression symbols commonly used in Python. The main symbols are:

.
By default, one character is matched, excluding line breaks. If DOTALL is set, the line breaks are matched.

^
Match the beginning of a row

$
Match the end of a row

*
Matches 0 or multiple duplicates.

+
Match one or more duplicates

?
Matches one or zero duplicates.

*?, + ?,??
Match by non-Greedy Mode

{M}, {m, n}, {m, n }?
Match m duplicates, m to n duplicates, and m to n duplicates in 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

(?

(? (Id/name) yes-pattern | no-pattern)
\ Number
\ A matches the start of A string
\ B match word boundary

\ B
\ B's antsense

\ D indicates [0-9]
\ D indicates [^ 0-9]
\ S indicates [\ t \ r \ n \ f \ v]
\ S is a non-white space character
\ W is equivalent to [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.