如何编写一个匹配所有没有'aaa'的字符序列的正则表达式 [英] How to write a regex which matches all char-sequences without 'aaa'

查看:50
本文介绍了如何编写一个匹配所有没有'aaa'的字符序列的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用负向前瞻来编写它,但以下正则表达式不起作用:

I tried to write that with negative lookahead, but the following regex doesn't work:

^.+(?!aaa).+$

它对所有序列进行数学运算.如何修改?

it mathces all sequences at all. How to modify that?

推荐答案

您需要做的就是将 .* 添加到展望组

All you need to do is to add a .* to the look ahead group

^(?!.*aaa).+$

  • (?!.*aaa) 否定前瞻确保字符串中没有aaa
    • (?!.*aaa) Negative look ahead ensures that there is no aaa in the string
    • 正则表达式演示

      ^.+(?!aaa).+$

      • . 将匹配字符串中的第一个字符.

      • . will match the first character in the string.

      (?!aaa) 检查第一个字符后面是否没有aaa.显然,这不是我们所期望的.相反,我们需要在整个字符串中搜索序列,而不是将搜索限制在第一个字符之后.

      (?!aaa) checks if the first character is not followed by aaa. Clearly this is not something we expect. Rather we need to search the entire string for the sequance and not to limit the search after the first character.

      在哪里

      • (?!.*aaa) 将搜索缩放到整个字符串.
      • (?!.*aaa) scales the search to the entire string.

      这篇关于如何编写一个匹配所有没有'aaa'的字符序列的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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