Python re module, pythonre
1. Single Character matching
| Character |
Function |
| . |
Match any 1 character (except \ n) |
| [] |
Match the characters listed in [] |
| \ D |
Match Number, that is, 0-9 |
| \ D |
Match non-numbers, that is, not numbers |
| \ S |
Matches blank space, that is, space, tab key, \ n |
| \ S |
Match non-Blank |
| \ W |
Match word characters, that is, a-z, A-Z, 0-9 ,_ |
| \ W |
Match non-word characters |
2. Number
| Character |
Function |
| * |
Match the first character 0 times or unlimited times. |
| + |
Match the first character once or infinitely, that is, at least once |
| ? |
Match the previous character once or 0, that is, either once or no |
| {M} |
Match the previous character m times |
| {M ,} |
Match the first character at least MB |
| {M, n} |
Match the first character from m to n times |
Iii. Boundary Representation
| Character |
Function |
| ^ |
Match the start of a string |
| $ |
Matches the end of a string |
| \ B |
Match the boundary of a word |
| \ B |
Match non-word boundary |
Iv. Grouping
| Character |
Function |
| | |
Match any expression on the left or right |
| (AB) |
Use the characters in parentheses as a group |
\num |
Reference strings matched by group num |
(?P<name>) |
Group alias |
| (? P = name) |
Reference the strings matched by the name group with the alias |
5. Logo
| Modifier |
Description |
| Re. I |
Make matching case insensitive |
| Re. L |
Perform locale-aware matching |
| Re. M |
Multi-row matching, affecting ^ and $ |
| Re. S |
Make. Match All characters including line breaks |
| Re. U |
Parses Characters Based on the Unicode Character Set. This flag affects \ w, \ W, \ B, \ B. |
| Re. X |
This flag gives you a more flexible format so that you can write regular expressions more easily. |
6. match Method
Match (pattern, string, flags = 0) Try to apply the pattern at the start of the string, returning matches a match object from the string header, or None if no match was found.
| Parameters |
Description |
| Pattern |
Matched Regular Expression |
| String |
The string to be matched. |
| Flags |
Flag, used to control the matching mode of regular expressions, such as case-sensitive or multi-row matching. |
1 import re 2 3 ret = re. match ('com ', 'www .baidu.com') 4 print (ret) # None 5 6 ret = re. match ('(w)', 'www .baidu.com ') 7 print (type (ret) # <class' _ sre. SRE_Match '> 8 9 ''' 10 returns the Match object 11 attribute: 12 string: the text used for matching. 13 re: Specifies the Pattern object used for matching. 14 pos: Index of the regular expression in the text to start searching. The value is the same as that of the Pattern. match () and Pattern. seach () methods. 15 endpos: The index of the ending search by a regular expression in the text. The value is the same as that of the Pattern. match () and Pattern. seach () methods. 16 lastindex: The index of the last captured group in the text. If no captured group exists, the value is None. 17 lastgroup: the alias of the last captured group. If this group does not have an alias or is not captured, it is set to None. 18 method: 19 group ([group1,…]): 20. Obtain one or more strings intercepted by groups. If multiple parameters are specified, the strings are returned as tuples. Group1 can be numbered or alias. number 0 indicates the entire matched substring. If no parameter is set, group (0) is returned. If no string is intercepted, None is returned; the group that has been intercepted multiple times returns the last intercepted substring. 21 groups ([default]): 22 returns the string intercepted by all groups in the form of tuples. It is equivalent to calling group (1, 2 ,... Last ). Default indicates that the group that has not intercepted the string is replaced by this value. The default value is None. 23 groupdict ([default]): 24 returns a dictionary with the alias of an alias group as the key and the substring intercepted by this group as the value. groups without aliases are not included. The meaning of default is the same as that of default. 25 start ([group]): 26 returns the starting index of the substring intercepted by the specified group in the string (index of the first character of the substring ). The default value of group is 0. 27 end ([group]): 28 returns the end index of the substring intercepted by the specified group in the string (index of the last character of the substring + 1 ). The default value of group is 0. 29 span ([group]): 30 returns (start (group), end (group )). 31 expand (template): 32. Place the matched group into the template and return the result. You can use \ id, \ g <id>, \ g <name> to reference groups in template, but cannot use number 0. \ Id and \ g <id> are equivalent, but \ 10 will be considered as 10th groups. If you want to express \ 1 followed by the character '0 ', only \ g <1> 0 can be used. 33 ''' 34 35 print (ret. lastgroup) # None36 print (ret. lastindex) #337 print (ret. pos) #038 print (ret. endpos) #1339 print (ret. re) # re. compile ('(w)') 40 print (ret. regs) # (0, 3), (0, 1), (1, 2), (2, 3) 41 print (ret. string) # www. baidu. com42 43 print (ret. start () #044 print (ret. end () #345 print (ret. span () # (0, 3) 46 print (ret. group () # www47 print (ret. groups () # ('w', 'w', 'w') 48 print (ret. groupdict () # {} 49 print (ret. expand (R' \ 1-\ 2-\ 3') # w-w
VII. search Method