<h3>Python中的re模塊</h3>
Python中提供perl風格的正則表達式模式,re模塊使Python擁有全部的正則表達式功能富寿。
compile函數(shù)根據(jù)一個模式字符串和可選標志參數(shù)生成一個正則表達式對象,這個對象擁有一系列方法用于正則表達式匹配和替換轩缤。
re模塊提供了與這些方法功能完全一致的函數(shù)酒繁,這些函數(shù)使用一個模式字符串作為他們的第一個參數(shù)。
本文介紹re模塊的主要函數(shù)和用法捶索。
re.py中關于一些特殊字符的定義
r"""
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.
"$" 匹配字符串結(jié)尾或字符串結(jié)尾換行符之前
"*" Matches 0 or more (greedy) repetitions of the preceding RE.Greedy means that it will match as many repetitions as possible.
"*" 匹配0或者 更多(貪婪)。貪婪意味著它會盡可能的匹配更多
"+" Matches 1 or more (greedy) repetitions of the preceding RE.
"+" 匹配1或者更多灰瞻。
"?" Matches 0 or 1 (greedy) of the preceding RE.
"?" 盡可能的匹配1或者0
*?,+?,?? 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.""
r"""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 匹配任何十進制整數(shù);相當于字節(jié)模式的集合[0-9]或者有ASCII標志的字符串模式腥例。在沒有ASCII標志的字符串模式辅甥,它將匹配所有的Unicode整數(shù)
\D Matches any non-digit character; equivalent to [^\d].
\D 匹配任何非數(shù)字字符;相當于[^\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 匹配任何空格字符;相當于具有ASCII標志的字節(jié)模式或字符串模式中的[\ t \ n \ r \ f \ v]燎竖。在沒有ASCII標志的字符串模式中璃弄,它將匹配Unicode空格字符的整個范圍。
\S Matches any non-whitespace character; equivalent to [^\s].
\S 匹配任何非空字符构回;相當于[^\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 配任何字母數(shù)字字符;相當于[a-zA-Z0-9_]字節(jié)模式或帶ASCII標志的字符串模式。在沒有ASCII標志的字符串模式中纤掸,它將匹配Unicode字母數(shù)字字符(字母加數(shù)字和下劃線)的范圍脐供。使用LOCALE,它將匹配定義為當前語言環(huán)境的字母的集[0-9_]加字符借跪。
\W Matches the complement of \w.
\W 匹配\w的補碼
\\ Matches a literal backslash.
\\ 匹配反斜杠
python的re模塊包含以下函數(shù):
r"""
match Match a regular expression pattern to the beginning of a string.
match 從字符串開頭匹配正則表達式政己。
fullmatch Match a regular expression pattern to all of a string.
fullmatch 從整個字符串匹配正則表達式
search Search a string for the presence of a pattern.
search
sub Substitute occurrences of a pattern found in a string.
sub 替換在字符串
subn Same as sub, but also return the number of substitutions made.
subn 和sub一樣,但是返回所做替換的次數(shù)
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."""
該模塊中的一些功能將標志作為可選參數(shù):
r"""
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."""
re模塊中同樣定義了一個異常"錯誤".掏愁。