Python Regular Expressions

Source: Internet
Author: User

Mode Description
^ Matches the beginning of a string
$ Matches the end of the string.
. Matches any character, except the newline character, when re. When the Dotall tag is specified, it can match any character that includes a line feed.
[...] Used to represent a set of characters, listed separately: [AMK] matches ' a ', ' m ' or ' K '
[^...] Characters not in []: [^ABC] matches characters other than a,b,c.
Tel Matches 0 or more expressions.
Tem Matches 1 or more expressions.
Re? Matches 0 or 1 fragments defined by a preceding regular expression, not greedy
re{N,} Exact match n preceding expression.
re{N, m} Matches N to M times the fragment defined by the preceding regular expression, greedy way
a| B Match A or B
(RE) The G matches the expression in parentheses, and also represents a group
(? imx) The regular expression consists of three optional flags: I, M, or X. Affects only the areas in parentheses.
(?-imx) The regular expression closes I, M, or x optional flag. Affects only the areas in parentheses.
(?: RE) A similar (...), but does not represent a group
(? imx:re) Use I, M, or x optional flag in parentheses
(?-imx:re) I, M, or x optional flags are not used in parentheses
(?#...) Comments.
(? = re) Forward positive qualifiers. If a regular expression is included, ... Indicates that a successful match at the current position succeeds or fails. But once the contained expression has been tried, the matching engine is not improved at all, and the remainder of the pattern attempts to the right of the delimiter.
(?! Re) Forward negative qualifier. As opposed to a positive qualifier, when the containing expression cannot match the current position of the string
(?> re) Match the standalone mode, eliminating backtracking.
\w Match Alpha-Numeric
\w Match non-alphanumeric numbers
\s Matches any whitespace character, equivalent to [\t\n\r\f].
\s Match any non-null character
\d Match any number, equivalent to [0-9].
\d Match any non-numeric
\a Match string start
\z Matches the end of the string, if there is a newline, matches only the ending string before the line break. C
\z Match string End
\g Matches the position where the last match was completed.
\b Matches a word boundary, which is the position between a word and a space. For example, ' er\b ' can match ' er ' in ' never ', but not ' er ' in ' verb '.
\b Matches a non-word boundary. ' er\b ' can match ' er ' in ' verb ', but cannot match ' er ' in ' Never '.
\ n, \ t, et. Matches a line break. Matches a tab character. such as
\1...\9 A sub-expression that matches the nth grouping.
\10 Matches the sub-expression of the nth grouping if it is matched. Otherwise, it refers to an expression of octal character code.


1) Match abc>>> import re>>> m = Re.match ("abc", "ABCdef") >>> print (m) <_sre. Sre_match object; Span= (0, 3), match= ' abc ' >>>> m = Re.match ("abc", "Babcdef") #match can only be matched from scratch >>> print (m) None2) number of matches Fixed >>> m = Re.match ("[0-9]", "12313abcdef") >>> print ( ()) 1>>> m = Re.match ("[0-9]{0,3}" , "123456789abcdef") # matches 0-3 times >>> print ( ()) 123>>> m = Re.match ("[0-9]{5}", "123456789abcdef") # Match 5 times >>> print ( ()) 12345


>>> m = Re.findall ("[0-9]{1,10}", "1234ab5cde6f") #匹配所有 >>> print (m) [' 1234 ', ' 5 ', ' 6 ']>>> m = Re.findall ("[a-za-z]{1,10}", "1234ab5cde6f") >>> print (m) [' Ab ', ' cde ', ' f ']>>> m = Re.findall (". *", " 1234ab5cde6f ") >>> print (m) [' 1234ab5cde6f ', ']>>> m = Re.findall (". + "," 1234ab5cde6f ") >>> Print (m) [' 1234ab5cde6f ']

>>> m = ("abc", "Babcdef") #查找匹配的第一个 >>> print ( ()) ABC


>>> m = re.sub ("\d+", "|", "B1SD3BCD6EF") #所有的数字替换为 |>>> print (m) b|sd|bcd|ef>>> m = re.sub ("\d + "," | "," B1SD3BCD6EF ", count=2) #只替换2个 >>> print (m) b|sd|bcd6ef

This article is from the "AMY" blog, please make sure to keep this source

Python Regular Expressions

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: 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.