您的位置:首页 > 编程语言 > Python开发

浅析python正则表达式一:基本概念

2016-01-20 00:00 1021 查看
摘要: 根据python的re模块的帮助文档,简要总结python正则中常用的特殊字符,集合简写和正则表达式函数,最好的学习资料就是python自带文档。正则表达式的水很深,关键是要多实践。

该文来自我的个人博客, http://zpeng.gitcafe.io/2016/01/19/python/re-python/, 转载请注明,谢谢。

前言

断断续续花了一个多月把,hackerrank上关于python正则部分的题刷完了,对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的用法
在集合简写这一块中,比较难的的是\number的正确使用

例1:查找字符串中是否有字符重复出现
In [39]: bool(re.search(r'(.).*\1','asdfasjkdlk'))
Out[39]: True


例2: 查找字符串中是否有字符连续出现四次
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/, 转载请注明,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 正则表达式