Python Regular Expressions

Source: Internet
Author: User

The primary function of regular expressions (regular expression) is to search for what you want to find by using a specific pattern (pattern) from a string .

Using regular expressions in Python requires a package re in the standard library.

5 common types of operations for regular expressions

Re.match (Pattern, string) # match from the beginning

m = Re.match ("[0-9]", "756abc6def")    #[0-9] matches only one character print (M.group ()) #输出 7m = Re.match ("[0-9][0-9]", "75678abc6def") Print (M.group ()) #输出 75m = Re.match ("[0-9]{0,10}", "75678abc6def")    #{0,10} means matching 0 to 10 print (M.group ()) #输出 75678

Re.findall (Pattern, String) # finds all characters to match and returns the list format

m = Re.findall ("[0-9]{1,10}", "7578abc6def78sjdo")    #匹配所有的数字if  m:    print (m) #输出    [' 7578 ', ' 6 ', ' + ']m = Re.findall ("[a-za-z]{1,10}", "7578abac6def78sjddo")    #匹配所有的字母if  m:    print (m) #输出    [' AbAc ', ' def ', ' Sjddo ']

Re.search (Pattern, String) # matches the entire string until a match is found

m = Re.search ("\d+", "Abac65def78sjddo")    if  m:    print (M.group ()) #输出    65

Re.sub (Pattern, Repl, String, Count,flag) # replaces the matched character

m = re.sub ("\d+", "|", "Abac65def78sjddo")     #找到所有的数字, replace with | If  m:    print (m) #输出    abac|def|sjddom = Re.sub ("\d+", "|", "Abac65def78sjddo", count=1)     #替换一次if  m:    print (m) #输出    abac|def78sjddom = Re.sub ("\s*", "", "AB  a8s   jD do   ")     #找到所有的空格并删除if  m:    print (m) #输出    Aba8sjddo

Re.split (Pattern, String) # splits the matched format into a list of strings as split points

m = Re.split (' [0-9] ', ' Alex1tony2falsh3rambo ') print (m) #输出    [' Alex ', ' Tony ', ' Falsh ', ' Rambo ']

Match format

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

  

A few common regular examples

1. Match phone number

PHONE_STR = "Hey My name is Alex, and my phone number was 13651054607, please call me if you are pretty!" PHONE_STR2 = "Hey My name is Alex, and my phone number was 18651054604, please call me if you are pretty!" # "(1) ([358]\d{9})", (1) indicates that the first bit is 1,[358] means that the second bit is 3 or 5 or 8,\d{9} and that the remaining 9 digits m = Re.search ("(1) ([358]\d{9})", PHONE_STR2) if M:    print (M.group ()) #输出    18651054604

2. Match IP Address

ip_addr = "inet 192.168.60.223 netmask 0xffffff00 broadcast 192.168.60.255" #匹配四段1-3 digit number, does not judge the legality m = Re.search ("\d{1,3}\". \d{1,3}\.\d{1,3}\.\d{1,3} ", ip_addr) print (M.group ())

3. Match Email

email = "[email protected]   http://www.oldboyedu.com" m = Re.search (r "[0-9.a-z]{0,26}@[0-9.a-z]{0,20}.[ 0-9a-z]{0,8} ", email) print (M.group ())

  

 

 

  

Python Regular Expressions

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.