该文来自我的个人博客, http://zpeng.gitcafe.io/2016/01/19/python/re-python/, 转载请注明,谢谢。
断断续续花了一个多月把,hackerrank上关于python正则部分的题刷完了,对python正则表达式的认识达到了新的高度。写下博文记录一些python正则的技能点。
在ipython中输入help(re),查看正则模块re的帮组信息。
python正则中的特殊字符如下:
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. (?iLmsux) Set the 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.
总结如下:
符号 | 含义 |
---|---|
. | 匹配除了换行符的任意字符 |
^ | 字符串开头或 用在[]中表示不匹配某些字符 |
$ | 匹配模式的末尾 |
* | 匹配任意多的字符 |
+ | 至少匹配一个字符 |
? | 匹配0或一个字符 |
{m,n} | 表示重复前面模式m到n次,可表达为{m},{m,},{m,n} |
[] | 字符集合 |
() | 子模式 |
| | 模式或 |
?字符在正则表达式中功能特别丰富,看浅析python正则表达式二:用好问号。
查看re帮助文档,如下:
\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]. \D Matches any non-digit character; equivalent to the set [^0-9]. \s Matches any whitespace character; equivalent to [ \t\n\r\f\v]. \S Matches any non-whitespace character; equiv. to [^ \t\n\r\f\v]. \w Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]. 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.
总结如下:
简写 | 含义 |
---|---|
\number | 匹配模式组number |
\A | 等价于^ |
\Z | 等价于$ |
\b | 匹配单词开头结尾的空格 |
\B | 匹配的空格不在单词的开头结尾 |
\d | 等价于[0-9] |
\D | 等价于[^0-9] |
\s | 等价于[ \t\n\r\f\v] |
\S | 等价于[^ \t\n\r\f\v] |
\w | 等价于[a-zA-Z0-9_] |
\W | 等价于[^a-zA-Z0-9_] |
\ | 转义匹配\ |
在集合简写这一块中,比较难的的是\number的正确使用
In [39]: bool(re.search(r'(.).*\1','asdfasjkdlk')) Out[39]: True
In [40]: bool(re.search(r'(.)\1{3}','asdfaaaasjkdlk')) Out[40]: True
上面两个例子中\1表示该处出现的字符或字符串与匹配组1的字符或字符串相同,这里是(.)
帮助文档如下:
match Match a regular expression pattern to the beginning 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 | 返回所有的模式匹配字符串 |
finditer | 迭代返回match对象 |
complie | 编译模式 |
escape | 转义字符串中所有特殊字符,这样不用在字符串中写入讨厌的\符号 |
正则表达式函数都有个参数flag,其中令flage=re.I,表示对大小写不敏感。
python正则表达式的基本概念也就这些,学习这些的最好资料是自带的帮助文档。
该文来自我的个人博客, http://zpeng.gitcafe.io/2016/01/19/python/re-python/, 转载请注明,谢谢。