栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 正则表达式 - Regular expression

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 正则表达式 - Regular expression

Regular expression

A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).

常见正则表达式符号
限定符 (Quantifier)元字符(meta-characters)
a*a 出现0次或多次d匹配数字字符
a+a 出现1次或多次D匹配非数字字符
a?a 出现0次或1次w匹配单词字符(英文、数字、下划线)
a{6}a 出现6次W匹配非单词字符
a{2,6}a出现2-6次s匹配空白符(包含换行符、Tab)
a{2,}a出现两次以上S匹配非空白字符
或运算符 (Or Operator).匹配任意字符(换行符除外)
(a|b)匹配a或者bb标注字符的边界(全字匹配)
(ab)|(cd)匹配ab或者cd^匹配行首
字符类 (Character Classes)$匹配行尾
[abc]匹配a或者b或者c贪婪/懒惰匹配(Greedy/Lazy Match)
[a-c]同上<.+>默认贪婪匹配“任意字符”
[a-zA-Z0-9]匹配大小写字母以及数字<.+?>懒惰匹配“任意字符”
[^0-9]匹配非数字字符
拓展表示法
NotationDescriptionExample Regex
(?iLmsux)Embed one or more special “flags”parameters within the regex itself (vs. via function/method)(?x),(?im)
(?:…)Signifies a group whose match is not saved(?:w+.)*
(?P…)Like a regular group match only identified with name rather than a numeric ID(?P)
(?P=name)Matches text previously grouped by (?P)in the same string(?P=data)
(?#…)Specifies a comment, all contents within ignored(?#comment)
(?=…)Matches if…comes next without consuming input string; called positive lookahead assertion(?=.com)
(?!..)Matches if … doesn’t come next without consuming input; called negative lookahead assertion(?!.net)
(?<=…)Matches if … comes prior without consuming input string; called positive look behind assertion(?<=800-)
(?Matches if … doesn’t come prior without consuming input; called negative look behind assertion(?
(?(id/name)Y|N)Conditional match of regex Y if group with given id or name exists else N; N is optional(?(1)y|x)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/755350.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号