A brief analysis of common regular expression symbols in Python _python

Source: Internet
Author: User
Tags in python

The main understanding of the expression of Python is the understanding of the symbol, which is the simple analysis of regular expression symbols commonly used in Python. The main symbols are:

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

^
Match the beginning of a line

$
Match end of Line

*
Match 0 or more duplicates

+
Match one or more duplicates


Match one or 0 duplicates

*?,+?,??
Match in non-greedy mode

{m},{m,n},{m,n}?
Match M Repeat, M to n repeat, m to n repeat in non greedy mode

\
Escape

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

(? ilmsux)

(?:...) (? P<name> ...)
>>> Re.match ('? P<NAME>ABC) {2} ', ' Abcabc '). Groupdict ()
{' name ': ' ABC '}
(? P=name)
>>> Re.match (R ' (?) P<NAME>ABC)----(? P=name) ', ' ABC----ABC '). Group ()
' ABC----ABC '
(? # ...) #后面的内容为注释 (?
= ...)

Matches the contents of the string that need to match

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

(?!) ...)

Matching characters do not match the contents after

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

Matching strings need to match before

(?<!...) Matching characters cannot match before

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

\b
The \b of the anti-righteousness

\d said [0-9]
\d says [^0-9]
\s says [\t\r\n\f\v]
\s is not a blank character
\w equivalent to [a-za-z0-9]
The \w of the \w of the Yi-

End of \z 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.