Python3学习笔记-re模块

re模块

re模块是Python正则表达式功能的封装,提供Perl风格的正则表达式模式

re模块组成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class:
Match

| end(self, group=0, /)
| Return index of the end of the substring matched by group.
|
| expand(self, /, template)
| Return the string obtained by doing backslash substitution on the string template, as done by the sub() method.
|
| group(...)
| group([group1, ...]) -> str or tuple.
| Return subgroup(s) of the match by indices or names.
| For 0 returns the entire match.
|
| groupdict(self, /, default=None)
| Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name.
|
| default
| Is used for groups that did not participate in the match.
|
| groups(self, /, default=None)
| Return a tuple containing all the subgroups of the match, from 1.
|
| default
| Is used for groups that did not participate in the match.
|
| span(self, group=0, /)
| For match object m, return the 2-tuple (m.start(group), m.end(group)).
|
| start(self, group=0, /)
| Return index of the start of the substring matched by group.

Pattern

| findall(self, /, string, pos=0, endpos=9223372036854775807)
| Return a list of all non-overlapping matches of pattern in string.
|
| finditer(self, /, string, pos=0, endpos=9223372036854775807)
| Return an iterator over all non-overlapping matches for the RE pattern in string.
|
| For each match, the iterator returns a match object.
|
| fullmatch(self, /, string, pos=0, endpos=9223372036854775807)
| Matches against all of the string.
|
| match(self, /, string, pos=0, endpos=9223372036854775807)
| Matches zero or more characters at the beginning of the string.
|
| scanner(self, /, string, pos=0, endpos=9223372036854775807)
|
| search(self, /, string, pos=0, endpos=9223372036854775807)
| Scan through string looking for a match, and return a corresponding match object instance.
|
| Return None if no position in the string matches.
|
| split(self, /, string, maxsplit=0)
| Split string by the occurrences of pattern.
|
| sub(self, /, repl, string, count=0)
| Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
|
| subn(self, /, repl, string, count=0)
| Return the tuple (new_string, number_of_subs_made) found by replacing the leftmost non-overlapping occurrences of pattern with the replacement repl.

error

class static function:
compile(pattern, flags=0)#编译一个正则表达式,返回一个Pattern对象
Compile a regular expression pattern, returning a Pattern object.


escape(pattern) #将正则表达式中的元字符加上转义符号 再返回
Escape special characters in a string.


findall(pattern, string, flags=0)
#查找字符串中所有匹配的子串,返回一个列表

Return a list of all non-overlapping matches in the string.

If one or more capturing groups are present in the pattern, return
a list of groups; this will be a list of tuples if the pattern
has more than one group.

Empty matches are included in the result.

finditer(pattern, string, flags=0)
#查找字符串中所有匹配的子串,返回一个迭代器,迭代器存储的都是Match对象
Return an iterator over all non-overlapping matches in the
string. For each match, the iterator returns a Match object.

Empty matches are included in the result.

fullmatch(pattern, string, flags=0)
#尝试匹配整个字符串,返回Match对象,或者None
Try to apply the pattern to all of the string, returning
a Match object, or None if no match was found.

match(pattern, string, flags=0)
#从头开始匹配字符串,返回Match对象,或者None 只查找一次
Try to apply the pattern at the start of the string, returning
a Match object, or None if no match was found.

purge()
#清除正则表达式缓存
Clear the regular expression caches

search(pattern, string, flags=0)
#查找整个匹配的字符串,返回Math对象,或者None 值查找一次
Scan through string looking for a match to the pattern, returning
a Match object, or None if no match was found.

split(pattern, string, maxsplit=0, flags=0)
#根据给定的正则表达式,切割字符串,返回一个list
Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings. If
capturing parentheses are used in pattern, then the text of all
groups in the pattern are also returned as part of the resulting
list. If maxsplit is nonzero, at most maxsplit splits occur,
and the remainder of the string is returned as the final element
of the list.

sub(pattern, repl, string, count=0, flags=0)
#根据给定的正则表达式,给定的子串,替换字符串中的匹配字符成子串,返回匹配的子串
Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a string, backslash escapes in it are processed. If it is
a callable, it's passed the Match object and must return
a replacement string to be used.

subn(pattern, repl, string, count=0, flags=0)
#与sub相似,返回一个二元tuple
Return a 2-tuple containing (new_string, number).
new_string is the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in the source
string by the replacement repl. number is the number of
substitutions that were made. repl can be either a string or a
callable; if a string, backslash escapes in it are processed.
If it is a callable, it's passed the Match object and must
return a replacement string to be used.

template(pattern, flags=0)
#编译一个模板模式,返回一个Pattern对象
Compile a template pattern, returning a Pattern object