不包含特定字符串的正则表达式 [英] Regular expression that doesn't contain certain string
问题描述
我有这样的东西
aabbabcaabda
aabbabcaabda
为了选择由 a 包裹的最小组,我有这个 /a([^a]*)a/
效果很好
for selecting minimal group wrapped by a I have this /a([^a]*)a/
which works just fine
但是我对 aa 包裹的组有问题,我需要类似的东西/aa([^aa]*)aa/
这不起作用,我不能像 /aa([^a]*)aa/那样使用第一个code>,因为它会在第一次出现 a 时结束,这是我不想要的.
But i have problem with groups wrapped by aa, where I'd need something like
/aa([^aa]*)aa/
which doesn't work, and I can't use the first one like /aa([^a]*)aa/
, because it would end on first occurence of a, which I don't want.
一般来说,有没有什么办法,用同样的方式说not contains string我可以用 [^a]
说 not contains character 吗?
Generally, is there any way, how to say not contains string in the same way that
I can say not contains character with [^a]
?
简单地说,我需要 aa 后跟除序列 aa 之外的任何字符,然后以 aa
Simply said, I need aa followed by any character except sequence aa and then ends with aa
推荐答案
一般来说,编写一个不包含特定字符串的正则表达式是一件很痛苦的事情.对于计算模型,我们必须这样做——你使用一个很容易定义的 NFA,然后将其简化为正则表达式.不包含cat"的事物的表达大约有 80 个字符长.
In general it's a pain to write a regular expression not containing a particular string. We had to do this for models of computation - you take an NFA, which is easy enough to define, and then reduce it to a regular expression. The expression for things not containing "cat" was about 80 characters long.
我刚刚完成,是的,它是:
I just finished and yes, it's:
aa([^a] | a[^a])aa
这里是一个非常简短的教程.我之前发现了一些很棒的,但现在看不到了.
Here is a very brief tutorial. I found some great ones before, but I can't see them anymore.
这篇关于不包含特定字符串的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!