Re module
Used to match an operation in a regular expression.
In Python, in order to avoid the implementation of the output ' \ ', the ' \ n ' character escape problem (such as regular expressions using backslashes "\" to represent a special form or as an escape character, here with the syntax of the Python conflict, so Python uses "\\\\" to denote "\" in the regular expression, Because if you want to match "\" in the regular expression, you need to escape it with \ to become "\ \", and the Python syntax needs to escape every \ in the string, so it becomes "\\\\". )。
To make the regular expression more readable, Python specifically designed the original string (raw string), but do not use raw string when writing the file path, there is a trap. Raw string is prefixed with ' R ' as a string, such as r "\ n": two characters "\" and "n" instead of line breaks. This form is recommended when writing regular expressions in Python.
For more information on regular expressions, see: http://jinyudong.blog.51cto.com/10990408/1910334
For the RE module, Python provides two different primitive operations: match and search.
Match is matched from the beginning of the string, and search (perl default) makes any match from the string.
Note: When the regular expression starts with ' ^ ', match is the same as search (if ' ^ ' is not at the beginning, it represents only one ordinary character). Match succeeds only if and only if the matched string starts with a match or matches from the position of the POS parameter. As follows:
Example 1:
>>> Import re
>>> Re.match ("C", "Cabcdef")
<_sre. Sre_match Object at 0x00a9ab80>
>>> Re.search ("C", "Cabcdef")
<_sre. Sre_match Object at 0x00af1720>
>>> M=re.match ("^ab+", "ASDFABBBB")
>>> Print M
None
>>> m=re.search ("^ab+", "ASDFABBBB")
>>> Print M
None
>>> M=re.match ("ab+", "ASDFABBBB")
>>> Print M
None
>>> m=re.search ("ab+", "ASDFABBBB")
>>> Print M
<_sre. Sre_match Object at 0x011b1988>
What the output matches to
>>> Print M.group ()
abbbb
>>> patterm = Re.compile ("C")
>>> Patterm.match ("abcdef")
>>> Patterm.match ("abcdef", 1)
>>> Patterm.match ("ABCdef", 2)
<_sre. Sre_match Object at 0x00a9ab80>
Note: When the flag bit points to the character C, the output.
Module use:
Re.compile (pattern, flags=0)
Compiles a regular expression, returns a Regexobject object, and can then invoke the match () and the search () method through the Regexobject object.
Prog = Re.compile (pattern)
result = Prog.match (string)
With
result = Re.match (pattern, string)
is equivalent.
The first way to achieve the reuse of regular expressions.
Re.search (Pattern, string, flags=0)
Looks in the string to see if it matches the regular expression. Returns _SRE. The Sre_match object, if it cannot match, returns none.
Re.match (Pattern, string, flags=0)
Whether the beginning of the string can match the regular expression. Returns _SRE. The Sre_match object, if it cannot match, returns none.
See the example above in Example 1.
Re.split (Pattern, string, maxsplit=0)
Separates a string from a regular expression. If you enclose the regular expression in parentheses, the matching string is also returned in the list. Maxsplit is the number of separations, the maxsplit=1 is separated once, the default is 0, the number of times is not limited.
>>> re.split (' \w+ ', ' Words, Words, Words. ')
[' Words ', ' Words ', ' Words ', ']
>>> re.split (' (\w+) ', ' Words, Words, Words. ')
[' Words ', ', ', ' Words ', ', ', ' Words ', '. ', ']
>>> re.split (' \w+ ', ' Words, Words, Words. ', 1)
[' Words ', ' Words, Words. ']
>>> re.split (' [a-f]+ ', ' 0a3b9 ', flags=re. IGNORECASE)
Note: The python used here is 2.6, and viewing the source code finds that split () does not have the flags parameter and 2.7 increases. This problem I found more than once, the official documents and source inconsistencies, if found abnormal, should go to the source code to find the reason.
If it matches at the beginning or end of the string, the returned list will start or end with a blank string.
>>> re.split (' (\w+) ', ' ... words, words ... ')
[', ' ... ', ' words ', ', ', ' words ', ' ... ', ']
If the string does not match, a list of the entire string is returned.
>>> Re.split ("A", "BBB")
[' BBB ']
Re.findall (Pattern, string, flags=0)
Find all the substrings that the RE matches and return them as a list. This match is returned from left to right in an orderly manner. If there is no match, an empty list is returned.
>>> Re.findall ("A", "Bcdef")
[]
>>> Re.findall (r "\d+", "12A32BC43JF3")
[' 12 ', ' 32 ', ' 43 ', ' 3 ']
Re.finditer (Pattern, string, flags=0)
Find all the substrings that the RE matches and return them as an iterator. This match is returned from left to right in an orderly manner. If there is no match, an empty list is returned.
>>> it = Re.finditer (r "\d+", "12A32BC43JF3")
>>> for match in it:
Print Match.group ()
12
32
43
3
Re.sub (Pattern, Repl, String, count=0, flags=0)
Find all the substrings that the RE matches and replace them with a different string. The optional parameter count is the maximum number of times a pattern match is replaced, and count must be a non-negative integer. The default value is 0 to replace all matches. If there is no match, the string will return unchanged.
RE.SUBN (Pattern, Repl, String, count=0, flags=0)
The same as the Re.sub method, but returns a two-tuple that contains the new string and the number of substitution executions.
Re.escape (String)
To escape non-alphanumeric numbers in a string
Re.purge ()
Emptying regular expressions in the cache
Regular Expression Object
Re. Regexobject
Re.compile () returns the Regexobject object
Re. Matchobject
Group () returns a string that is matched by RE
Start () returns the position where the match started
End () returns the position of the end of the match
Span () returns a tuple containing the position of the match (start, end)
Sign
The compile flag allows you to modify some of the way regular expressions are run. In the RE module The logo can use two names, one is full name such as IGNORECASE, one is abbreviated, one letter form like I. (If you are familiar with Perl's pattern modifications, use the same letters in one letter; for example, re.) The abbreviated form of verbose is re. X. Multiple flags can be specified by bitwise or-ing them. such as Re. I | Re. M is set to the I and M flags:
I
IGNORECASE
Makes the match insensitive to case, and the character class and the string that match the letter are ignored when the case is written. For example, [A-z] can also match lowercase letters, Spam can match "Spam", "Spam", or "Spam". This lowercase letter does not take into account the current position.
L
LOCALE
Affects "W," W, "B, and" B, depending on the current localization setting.
Locales is a feature in the C language library and is used to help with programming that requires different languages to consider. For example, if you are working with French text, you want to use "w+ to match the text, but" W matches only the character class [a-za-z]; it does not match "é" or "?". If your system is properly configured and localized to French, the internal C function tells the program that "é" should also be considered a letter. The use of the LOCALE flag when compiling regular expressions will give you the ability to use these C functions to process "W" compiled objects, which will be slower, but will also be able to match the French text with "w+" as you would expect.
M
MULTILINE
(At this time ^ and $ will not be interpreted; they will be introduced in section 4.1.)
Use "^" to match only the beginning of the string, and $ to match only the end of the string and the end of the string directly before the line break (if any). When this flag is specified, "^" matches the start of the string and the beginning of each line in the string. Similarly, the $ metacharacters match the end of the string and the end of each line in the string (directly before each line break).
S
Dotall
Make the "." Special character match any character exactly, including line breaks; no this flag, "." matches any characters except line breaks.
X
VERBOSE
This flag is given by giving you a more flexible format so that you can write regular expressions much easier to understand. When the flag is specified, a white space character in the re string is ignored, unless the whitespace is in the character class or after the backslash, which allows you to organize and indent the re more clearly. It can also allow you to write comments to the RE, which are ignored by the engine; the comment is identified by the "#" sign, but the symbol cannot be followed by a string or backslash.
Knot If you can use a string method, do not select a regular expression, because the string method is simpler and faster.
Python--6, re module