正则表达式匹配 - 一个字母前面没有另一个字母 [英] Regex Matching - A letter not preceded by another letter
本文介绍了正则表达式匹配 - 一个字母前面没有另一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么可以是匹配 anystring
后跟 daily
但它不能匹配 daily
前跟 m
的正则表达式?
What could be regex which match anystring
followed by daily
but it must not match daily
preceded by m
?
例如它应该匹配以下字符串
For example it should match following string
beta.daily
abcdaily
dailyabc
每天
但一定不能匹配
每天
或abcmdaily
或mdailyabc
我尝试了以下和其他正则表达式,但每次都失败了:
I have tried following and other regex but failed each time:
r'[^m]daily'
:但与daily
不匹配r'[^m]?daily'
: 它匹配包含mdaily
的字符串,这不是预期的
r'[^m]daily'
: But it doesn't match withdaily
r'[^m]?daily'
: It match with string containingmdaily
which is not intended
推荐答案
只需在 daily
之前添加一个否定的lookbehind,(?<!m)d
:
Just add a negative lookbehind, (?<!m)d
, before daily
:
(?<!m)daily
零宽度负向后视,(?<!m)
,确保 daily
前面没有 m
.
The zero width negative lookbehind, (?<!m)
, makes sure daily
is not preceded by m
.
这篇关于正则表达式匹配 - 一个字母前面没有另一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文