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或者b\b标注字符的边界(全字匹配)
(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(?<!192.168.)
(?(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)
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值