Regular expressions are a powerful and standard way to search, replace, and parse complex strings, and the regular-related things in Python are all under the RE module.
1 common matches
^ Start of matching string
$ matches the end of a string
\b matches the boundary of a word
\d matches any number
\d matches any non-numeric character
X match an optional x (match 1 or 0 x characters)
x* matches 0 or more times X
x+ matches 1 or more times X
X{n,m} at least n times, up to M times x
(a|b|c) either match a, or match B, or match C
(x) typically represents a memory group, you can use the Re.search function to return the object's groups () function to get its value
2 General Purpose
#-------------------------------------------------------------------------------# Coding: utf-8# Purpose: Regular Expression # # Author: zdk## Created: 26/02/2013# Copyright: (c) zdk 2013#------------------------------------ ------------------------------------------- Import Reif __name__ = = ' __main__ ': addr = "BROAD ROAD apt.3" C6/>print (Re.sub ("ROAD", "rd", addr)) # BRD Rd apt.3 print (Re.sub (r "\broad\b", "rd", addr)) # BROAD Rd Apt.3 pattern = ". *b.* (ROAD)?" Print (Re.search (Pattern, "ROAD")) #None print (Re.search (Pattern, "B")) #<_sre. Sre_match Object at 0x0230f020>
(1) re.sub ("Road", "Rd", addr) searches the string addr with the Re.sub function, satisfies the "RD" substitution of the expression "road"
(2) Re.sub (r "\broad\b", "RD", addr), "\b" means "boundary of word", in Python, because the character "\" must be escaped in a string, this becomes very cumbersome, so Python uses the prefix r to indicate that all characters in the string are not escaped.
(3) Re.search (Pattern, "ROAD") re module has a search function, the function has two parameters, one is a regular expression, one is a string, the search function returns a number of methods can describe the matching object, if no match is found, Then none is returned.
3 loosely-Regular expressions
These are "compact" types of expressions, which are harder to read, and are not guaranteed to be remembered after a few months, even if the meaning of the expression is now clear. So Python allows users to use the so-called loose regular expression to complete the inline document needs, and the general expression has the following two aspects of the main difference
ignores whitespace characters. Space characters, tabs, and carriage returns do not match themselves (if you want to match a space character in a loose regular expression, you do not have to escape it by adding a backslash symbol in front of it)
Ignore comments. As with normal Python code, comments begin with the # sign, ending at the end of the line.
#松散带有内联注释的正则表达式 pattern = "" " ^ # Begin of String m{0,3} # 0 to 3 M (cm| cd| D? c{0,3}) #CM or CD or D or D 0 to 3 C $ #end of string "" " print (Re.search (Pattern," MCM ", re. VERBOSE)) #<_sre. Sre_match object at 0x021baf60> print (Re.search (Pattern, "M99", re. VERBOSE)) #None
(1) When using loose regular expressions, one of the most important things is that you must pass an extra parameter, re. VERBOSE, which is a constant of the RE module, indicates that the regular expression to be matched is a loose regular expression. The spaces and annotations of pattern are ignored, but at the same time they are more readable.
4 Case studies: Resolving phone numbers
You must match the following phone number:
800-555-1212
800 555 1212
800.555.1212
(800) 555-1212
1-800-555-1212
800-555-1212-1234
800-555-1212x1234
800-555-1212 ext.1234
Work (555,1212) #1234
Format is more, we need to know that 800 is the area code, the trunk number is 555, the other number of telephone number is 1212, for the extension number, we need to know that the extension number 1234
Phonepattern = Re.compile (R ' # don ' t match beginging of String (\d{3}) # 3 digits\d* #any number of non-digits (\d {3}) # 3 digits\d* #any number of non-digits (\d{4}) # 4 digits\d* #any number of non-digits (\d*) #any number O F digits ", re. VERBOSE) Print (Phonepattern.search (' 555.1212 #1234 '). Groups ()) # (' 800 ', ' 555 ', ' 1212 ', ' 1234 ')
Print (Phonepattern.search (' Work 555.1212 #1234 '). Groups ()) # (' 800 ', ' 555 ', ' 1212 ', ' 1234 ')
(1) A loose regular expression above, first match 3 numeric area code (not necessarily from the first character, so no ^), followed by matching any number of non-numeric characters, followed by matching the 3 number of the main line number, and then match any number of non-numeric characters, followed by matching 4 numbers, Then match any number of non-numeric characters, then match any number of extension numbers, and then use the Groups function to group, get the correct phone number.