Re module
Function: implements string matching.
Metacharacters description
\ will the next character marker, or a backward reference, or an octal escape character. For example, "\\n" matches \ n. "\ n" matches the line break. The sequence "\ \" matches "\" and "\ (" Matches "(". is equivalent to the concept of "escape character" in many programming languages.
^ matches the starting position of the input string. If the multiline property of the RegExp object is set, ^ also matches the position after "\ n" or "\ r".
$ matches the end position of the input string. If the multiline property of the RegExp object is set, $ also matches the position before "\ n" or "\ r".
* matches the preceding sub-expression any time. For example, zo* can match "Z", "Zo" and "Zoo". * Equivalent to {0,}.
+ matches the preceding subexpression one or more times (greater than or equal to 1 times). For example, "zo+" can Match "Zo" and "Zoo", but not "Z". + equivalent to {1,}.
? Matches the preceding subexpression 0 or one time. For example, "Do (es)?" You can match "do" in "do" or "does".?
{n} n is a non-negative integer. Matches the determined n times. For example, "o{2}" cannot match "O" in "Bob", but can match two o in "food".
{N,}n is a non-negative integer.} Match at least n times. For example, "o{2,}" cannot match "O" in "Bob", but can match all o in "Foooood". "O{1,}" is equivalent to "o+". "O{0,}" is equivalent to "o*".
{N,M}M and N are non-negative integers, where n<=m. Matches at least n times and matches up to M times. For example, "o{1,3}" will match the first three o in "Fooooood". "o{0,1}" is equivalent to "O?". Note that there can be no spaces between a comma and two numbers.
? When the character immediately follows any other restriction (*,+,?,{n},{n,},{n,m}), the matching pattern is non-greedy. The non-greedy pattern matches the searched string as little as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for the string "Oooo", "o+?" A single "O" will be matched, and "o+" will match all "O".
. Matches any single character except for "\ r \ n". To match any character that includes "\ r \ n", use a pattern like "[\s\s]".
Pattern matches the pattern and gets the match. The obtained matches can be obtained from the resulting matches collection, the Submatches collection is used in VBScript, and the $0...$9 property is used in JScript. To match the parentheses character, use "or".
(?:p Attern) does not obtain a match, matches the pattern but does not get a matching result and is not stored for later use. This is used in the or character "(|)" It is useful to combine the various parts of a pattern. For example, "Industr (?: y|ies)" is a more abbreviated expression than "industry|industries".
The (? =pattern) non-fetch match, positive pre-check, matches the lookup string at the beginning of any string that matches the pattern, and the match does not need to be fetched for later use. For example, "Windows (? =95|98| nt|2000) "Can match" windows "in" Windows2000 ", but does not match" windows "in" Windows3.1 ". Pre-checking does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check.
(?! pattern) non-fetch match, positive negation pre-check, matches the lookup string at the beginning of any mismatched pattern string, and the match does not need to be fetched for later use. For example, "Windows (?! 95|98| nt|2000) "Can match" windows "in" Windows3.1 ", but does not match" windows "in" Windows2000 ".
(? <=pattern) non-acquisition matches, reverse positive pre-check, similar to positive pre-check, just opposite direction. For example, "(? <=95|98| nt|2000) Windows can match "Windows" in 2000Windows, but not "windows" in "3.1Windows".
(<!pattern) Non-acquisition, reverse negation pre-check, similar to positive negative pre-check, except in the opposite direction. For example "(? <!95|98| nt|2000) Windows can match "Windows" in 3.1Windows, but not "windows" in "2000Windows".
X|y matches x or Y. For example, "Z|food" can match "Z" or "food" or "Zood" (please be cautious here). "(z|f) Ood" matches "Zood" or "food".
[XYZ] Character set. Matches any one of the characters contained. For example, "[ABC]" can Match "a" in "plain".
[^XYZ] negative character set. Matches any character that is not contained. For example, "[^ABC]" can match "Plin" in "plain".
A [A-z] character range. Matches any character within the specified range. For example, "[A-z]" can match any lowercase alphabetic character in the range "a" to "Z".
Note: The range of characters can be represented only if the hyphen is inside a character group and appears between two characters; If the beginning of the character group is out, only the hyphen itself can be represented.
[^a-z] negative character range. Matches any character that is not in the specified range. For example, "[^a-z]" can match any character that is not in the range "a" to "Z".
\b Matches a word boundary, that is, the position between the word and the space (that is, the "match" of the regular expression has two concepts, one is the matching character and the other is the matching position, where the \b is the matching position). For example, "er\b" can Match "er" in "never", but cannot match "er" in "verb".
\b Matches a non-word boundary. "er\b" can Match "er" in "verb", but cannot match "er" in "Never".
\CX matches the control character indicated by X. For example, \cm matches a control-m or carriage return. The value of x must be one of a-Z or a-Z. Otherwise, c is considered to be a literal "C" character.
\d matches a numeric character. equivalent to [0-9].
\d matches a non-numeric character. equivalent to [^0-9].
\f matches a page break. Equivalent to \x0c and \CL.
\ n matches a line break. Equivalent to \x0a and \CJ.
\ r matches a carriage return character. Equivalent to \x0d and \cm.
\s matches any invisible character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
\s matches any visible character. equivalent to [^ \f\n\r\t\v].
\ t matches a tab character. Equivalent to \x09 and \ci.
\v matches a vertical tab. Equivalent to \x0b and \ck.
\w matches any word character that includes an underscore. Similar but not equivalent to "[a-za-z0-9_]", where the "word" character uses the Unicode character set.
\w matches any non-word character. Equivalent to "[^a-za-z0-9_]".
\XN matches N, where n is the hexadecimal escape value. The hexadecimal escape value must be two digits long for a determination. For example, "\x41" matches "A". "\x041" is equivalent to "\x04&1". ASCII encoding can be used in regular expressions.
\num matches num, where num is a positive integer. A reference to the obtained match. For example, "(.) \1 "matches two consecutive identical characters.
\ n identifies an octal escape value or a backward reference. n is a backward reference if \ n is preceded by at least one of the sub-expressions obtained. Otherwise, if n is the octal number (0-7), N is an octal escape value.
\NM identifies an octal escape value or a backward reference. If at least NM has obtained a subexpression before \nm, then NM is a backward reference. If there are at least N fetches before \nm, then n is a backward reference followed by the literal m. If none of the preceding conditions are met, if both N and M are octal digits (0-7), then \nm will match the octal escape value nm.
\NML if n is an octal number (0-7) and both M and L are octal digits (0-7), the octal escape value NML is matched.
\un matches N, where N is a Unicode character represented by four hexadecimal digits. For example, \u00a9 matches the copyright symbol (©).
\< \> the Start (\<) and End (\>) of the matching word (word). For example, the regular expression \<the\> can match "the" in the string "for the wise", but cannot match "the" in the string "otherwise". Note: This meta-character is not supported by all software. The expression between and is defined as a group, and the characters that match the expression are saved to a staging area (up to 9 in a regular expression), which can be referenced using \1 to \9 symbols.
| Perform a logical or (or) operation on the two matching criteria. For example, the regular expression (Him|her) matches "It belongs to him" and "it belongs to her", but does not match "it belongs to them." Note: This meta-character is not supported by all software.
+ match 1 or more of that character just before it. For example, the regular expression + + matches 9, 99, 999, and so on. Note: This meta-character is not supported by all software.
? Match 0 or 1 of that character just before it. Note: This meta-character is not supported by all software.
{i} {i,j} matches a specified number of characters, which are defined in an expression preceding it. For example, the regular expression a[0-9]{3} can match the character "A" followed by a string of exactly 3 numeric characters, such as A123, A348, etc., but does not match A1234. A regular expression [0-9]{4,6} matches any 4, 5, or 6 consecutive
Modifier description
Re. I make the match case insensitive
Re. L do localization identification (locale-aware) matching
Re. M multiple lines match, affect ^ and $
Re. S make. Match all characters, including line breaks
Re. U resolves characters based on the Unicode character set. This sign affects \w, \w, \b, \b.
Re. X This flag is given by giving you a more flexible format so that you can write regular expressions more easily.
Common methods:
Re.compile: Compiles a regular expression to generate an object. Then match
Re.findall: Gets all the matching characters and returns a list of all matching strings.
Re.search: Finds the entire string and finds it by: variable. Group () returns the result value. Returned null none if not found
Re.sub: A string found based on a regular expression replaced with a new string that returns a result
Re.split: String segmentation, Str.split can only be separated by a delimiter, the regular segmentation may be divided according to a rule.
str1 = "III444ABCDDK12SDF98ADSF000PPP"
Re_split_result = Re.split (' \d+ ', str1,maxsplit=0) The last parameter is the number of times to match the rule, and the default is 0 to match all
Print (re_split_result) # [' III ', ' ABCDDK ', ' SDF ', ' ADSF ', ' PPP ']
Re.match: Finds the beginning of the string. Re.match ("\d+", str), no number returns none, through the variable. Group () returns the result value
Extracts the area code and local numbers from the matching string. Re.match (' ^ (\d{3})-(\d{3,8}) $ ', ' 010-12345 ')
Re.fullmatch (): The specified string to find must match exactly. Pattern = Re.compile ("O[gh]"), Print (Pattern.fullmatch ("og"))
. Group (): Print matching results as strings
. Groups (): Print matching results in tuples
. Groupdict (): Print a matching structure in a dictionary
Example:
1 Print(Re.search ('(? P<province>[0-9]{4}) (? P<city>[0-9]{2}) (? P<BIRTHDAY>[0-9]{4})','37148119930612345'). Groupdict ())2Print Result: {'Province':'3714',' City':'Bayi','Birthday':'1993'}3 Print(Re.search ('(? P<province>[0-9]{4}) (? P<city>[0-9]{2}) (? P<BIRTHDAY>[0-9]{4})','37148119930612345'). Groups ())4Printing results: ('3714','Bayi','1993')5 Print(Re.search ('(? P<province>[0-9]{4}) (? P<city>[0-9]{2}) (? P<BIRTHDAY>[0-9]{4})','37148119930612345'). Group ())6Printed results: 3714811993
Example: Matching ' \ '
1 re.split ('\\\\', R'c:\windows\system32') 2 Note: Be sure to use 'r' to identify the special characters in this string before the existence of the string, and match the pattern with 4 \
Python Learning-RE module