re库是Python里面负责正则表达(regular expression)的一个库。
维基百科对正则表达的定义:
In theoretical computer science
and formal language theory
, a
regular expression
(sometimes called a
rational expression
)
is a sequence of characters
that define a search pattern, mainly for use in pattern matching
with strings
, or string matching
, i.e. "find and replace"-like operations.
简而言之,正则表达式是一个字符串的搜索模式,本身也是一个字符串,而且语法和含义具有跨语言通用性,即大部分规定在很多语言里面都是相通的。
某种程度上可以把正则表达式理解成为一种语言,而组成这个语言最重要的就是元字符(
Metacharacter
)了。下面是从re文档copy的元字符列表:
The special characters are:
"." Matches any character except a newline.
"^" Matches the start of the string.
"$" Matches the end of the string or just before the newline at
the end of the string.
"*" Matches 0 or more (greedy) repetitions of the preceding RE.
Greedy means that it will match as many repetitions as possible.
"+" Matches 1 or more (greedy) repetitions of the preceding RE.
"?" Matches 0 or 1 (greedy) of the preceding RE.
*?,+?,?? Non-greedy versions of the previous three special characters.
{m,n} Matches from m to n repetitions of the preceding RE.
{m,n}? Non-greedy version of the above.
"\\" Either escapes special characters or signals a special sequence.
[] Indicates a set of characters.
A "^" as the first character indicates a complementing set.
"|" A|B, creates an RE that will match either A or B.
(...) Matches the RE inside the parentheses.
The contents can be retrieved or matched later in the string.
(?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
(?:...) Non-grouping version of regular parentheses.
(?P<name>...) The substring matched by the group is accessible by name.
(?P=name) Matches the text matched earlier by the group named name.
(?#...) A comment; ignored.
(?=...) Matches if ... matches next, but doesn't consume the string.
(?!...) Matches if ... doesn't match next.
(?<=...) Matches if preceded by ... (must be fixed length).
(?<!...) Matches if not preceded by ... (must be fixed length).
(?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
the (optional) no pattern otherwise.
The special sequences consist of "\\" and a character from the list
below. If the ordinary character is not on the list, then the
resulting RE will match the second character.
\number Matches the contents of the group of the same number.
\A Matches only at the start of the string.
\Z Matches only at the end of the string.
\b Matches the empty string, but only at the start or end of a word.
\B Matches the empty string, but not at the start or end of a word.
\d Matches any decimal digit; equivalent to the set [0-9] in
bytes patterns or string patterns with the ASCII flag.
In string patterns without the ASCII flag, it will match the whole
range of Unicode digits.
\D Matches any non-digit character; equivalent to [^\d].
\s Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
bytes patterns or string patterns with the ASCII flag.
In string patterns without the ASCII flag, it will match the whole
range of Unicode whitespace characters.
\S Matches any non-whitespace character; equivalent to [^\s].
\w Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
in bytes patterns or string patterns with the ASCII flag.
In string patterns without the ASCII flag, it will match the
range of Unicode alphanumeric characters (letters plus digits
plus underscore).
With LOCALE, it will match the set [0-9_] plus characters defined
as letters for the current locale.
\W Matches the complement of \w.
\\ Matches a literal backslash.
正则表达的强大之处在于处理字符串的灵活性和简约性,一行可以顶的上N行常规匹配代码。
举例:匹配email:\
w+([-+.]\w+)*@\w+([-.]\w+)*.\w+([-.]\w+)*
\w匹配单词字符,
([-+.]\w+)*表示可能含有以-或+或.开头后面跟若干字符,整个部分可能出现零至若干次。后面的类似。
Python re库提供了几个函数,用来实现正则表达式的各种功能,包括查找、匹配、分割等等。文档同样提供了函数列表:
match Match a regular expression pattern to the beginning of a string.
fullmatch Match a regular expression pattern to all of a string.
search Search a string for the presence of a pattern.
sub Substitute occurrences of a pattern found in a string.
subn Same as sub, but also return the number of substitutions made.
split Split a string by the occurrences of a pattern.
findall Find all occurrences of a pattern in a string.
finditer Return an iterator yielding a match object for each match.
compile Compile a pattern into a RegexObject.
purge Clear the regular expression cache.
escape Backslash all non-alphanumerics in a string.
常用的主要有match,search,sub,split,findall。在介绍这些函数之前,先看看re库提供了哪些flags,即特殊匹配模式,比如可以指定忽略大小写,多行匹配,点符号能不能匹配新行等等:
Some of the functions in this module takes flags as optional parameters:
A ASCII For string patterns, make \w, \W, \b, \B, \d, \D
match the corresponding ASCII character categories
(rather than the whole Unicode categories, which is the
default).
For bytes patterns, this flag is the only available
behaviour and needn't be specified.
I IGNORECASE Perform case-insensitive matching.
L LOCALE Make \w, \W, \b, \B, dependent on the current locale.
M MULTILINE "^" matches the beginning of lines (after a newline)
as well as the string.
"$" matches the end of lines (before a newline) as well
as the end of the string.
S DOTALL "." matches any character at all, including the newline.
X VERBOSE Ignore whitespace and comments for nicer looking RE's.
U UNICODE For compatibility only. Ignored for string patterns (it
is the default), and forbidden for bytes patterns.
然后看几个函数:
match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.
最基本的匹配的函数,接受三个参数,功能是从字符串开头开始匹配。也就是说,想要的pattern必须从字符串开头就符合,否则就算中间出现了,也不会认为是match。例如:
>>> text = "JGood is a handsome boy, he is cool, clever, and so on..."
>>> m = re.match(r"(\w+)\s", text)
>>> m
<_sre.SRE_Match object; span=(0, 6), match='JGood '>
>>> m.groups()
('JGood',)
>>> m.span()
(0, 6)
>>> m = re.match(r"\s(\w+)", text)
>>> m # m is none
其中,pattern前面的r是raw的意思,用来避免转义,比如要匹配的是\n,假如没有r,输入的时候自动就当做换行处理了。当然这个例子加不加无所谓。
返回的是一个matchObject,可以用groups()函数将match结果以tuple形式返回。同样还可以用span()函数得到match结果在原字符串的index范围。
假如想要设置flags,在后面加上re.+匹配模式,例如
Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found.
相当于整体版的match:
>>> m = re.search(r"\s(\w+)", text)
>>> m
<_sre.SRE_Match object; span=(5, 8), match=' is'>
值得注意的是,search找到一个就返回了,如果想找到全部,用后面的findall。
split(pattern, string, maxsplit=0, flags=0)
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.
分割函数,只不过是用正则表达式来分割。返回一个包含分割结果的list。
>>> m = re.split(r"\s", text)
>>> m
['JGood', 'is', 'a', 'handsome', 'boy,', 'he', 'is', 'cool,', 'clever,', 'and', 'so', 'on...']
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.
替代函数。将符合pattern的表达式用提供的字符串替换,还可以指定替换几个。
>>> m = re.sub(r"\s", "+", text, 3)
>>> m
'JGood+is+a+handsome boy, he is cool, clever, and so on...'
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.
匹配全部能够匹配的,不像search找到一个就返回,而是搜索完毕整个字符串。
>>> m = re.findall(r"(\w+)\s", text)