正则表达式匹配 - 一个字母前面没有另一个字母 [英] Regex Matching - A letter not preceded by another letter

查看:58
本文介绍了正则表达式匹配 - 一个字母前面没有另一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么可以是匹配 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 with daily
  • r'[^m]?daily' : It match with string containing mdaily 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆