Article From:https://www.cnblogs.com/caochucheng/p/9971332.html

Author: Chu Ge

2018-11-16

IDE： Pycharm2018.02   Python 3.7

KeyWord :  Regular expression Re

Explain:

————————————————-

``` 1 # coding=utf-8
2 #---------------------------------
3 '''
4 # Author  : chu ge
5 # Function: Re Regular expression 6 #
7 '''
8 #---------------------------------
9
10 # -----------------------
11 '''
12
13 # Matching operation using match method14 # result = re.match(Regular expressions, strings to match)15
16 # If the previous step matches the data, the group method can be used to extract the data.17 # result.group
18
19 Original character20 r" "
21
22 Single character matching23
24 .   Match any one character except\n25 []  Match the characters listed in []26 \d  Matching numbers, i.e. 0-927 \D  Matching non-numerals, i.e. non-numerals28 \s  Match blanks, that is, spaces, tab keys29 \S  Matching non-whitespace30 \w  Match word characters, namely a-z, A-Z31 \W  Matching non-word characters32
33 Multi-character matching34
35 *   If the previous character is matched zero or infinite times, it will be available or not.36 +   Match the previous character once or infinitely, that is, at least once37 ？  Matching the previous character occurs once or 0 times, either once or infinitely38
39 {m} Match the previous character m times40 {m.}Match the previous character at least m times41 {m,n}Matching the previous character occurs from m to N times42
43
44 Boundary representation45
46 ^   Match the beginning of the string47 \$   Match the end of the string48 \b  Match the boundaries of a word49 \B  Matching non-word boundaries50
51
52 Matching grouping53
54 |       Match any left or right expression55 (ab)    Group the characters in parentheses as a group56 \num    Reference to the string matched by grouping num57 (?p<name>) Group aliases58 (?P<name>) Reference to a string matched by an alias name grouping59
60
61
62 '''
63 # ------------------------
64 import re
65
66
67
68 '''
69 # ============================================================================
70 #   Test specific71 # ============================================================================
72 '''
73 if __name__ == "__main__":
74     #matching
75     # result = re.match(r'<(?P<name1>\w*)><(?P<name2>\w*)>.*</(?P=name2)></(?P=name1)>','<html><h1>www.itcast.cn</h1></html>')
76     # print(result)
77     # result1 = result.group()
78     # print(result1)
79
80
81     # search
82     result = re.search(r"\d+,\b\d+","Reading times were 12345, 9999")
83     print(result)
84     result1 = result.group()
85     print(result1)
86
87
88     # Total statistics
89     # result = re.findall(r"\d+","99 3344, 223")
90     # print(result)
91
92     # replace
93     # result = re.sub(r"\d","195","4999")
94     # print(result)```

————————————————-