Brief analysis of common regular expression symbols in Python

Source: Internet
Author: User
The understanding of the expression in Python is mainly the understanding of the symbol, which is a simple analysis of the regular expression symbols commonly used in Python. The main symbols are:

.
Matches a character by default, does not contain newline characters, and matches line breaks if set Dotall

^
Match beginning

$
Match end of Line

*
Match 0 or more repetitions

+
Match one or more duplicates


Match one or 0 duplicates

*?,+?,??
Match by non-greedy pattern

{m},{m,n},{m,n}?
Match M repeats, M to n repeats, M to n repeats in non-greedy mode

\
Escape

[]
[Abc],[a-z][^a-z]
|
or match ' a|b '
(...)
Matching groups

(? 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 content following the matched string needs to be matched

>>> Re.match (R ' Phone (=\d{3}) ', ' phone123 '). Group () ' Phone ' # (?! ...)

Matches the character after the content does not match

>>> Re.match (R ' Phone (?! \d{3}) ', ' phoneabc123 '). Group () ' Phone ' (? <= ...)

Matching strings need to be matched in front of

(?

(? (id/name) Yes-pattern|no-pattern)
\number
\a matches the beginning of a string
\b Match word boundaries

\b
\b's anti-righteousness

\d = [0-9]
\d said [^0-9]
\s said [\t\r\n\f\v]
\s as non-whitespace characters
\w equivalent to [a-za-z0-9]
\w \w's anti-righteousness

\z the end of the matching string

  • Related Article

    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.