正则表达式来匹配的东西,不是一个具体的子 [英] Regex to match against something that is not a specific substring
问题描述
我要寻找一个正则表达式,将匹配一个字符串开始,不具有一定的子结束串
I am looking for a regex that will match a string that starts with one substring and does not end with a certain substring.
例如:
// Updated to be correct, thanks @Apocalisp
^foo.*(?<!bar)$
应该匹配任何以foo的开始,不与酒吧结尾。我了解[^ ...]语法,但我无法找到任何东西,会做一个字符串,而不是单个字符。
Should match anything that starts with "foo" and doesn't end with "bar". I know about the [^...] syntax, but I can't find anything that will do that for a string instead of single characters.
我特别想对Java的正则表达式做到这一点,但我碰到的这个答案,这样对其他的正则表达式引擎之前,将是巨大的。
I am specifically trying to do this for Java's regex, but I've run into this before so answers for other regex engines would be great too.
感谢@Kibbee验证这个工作在C#以及。
Thanks to @Kibbee for verifying that this works in C# as well.
推荐答案
我想你想这种情况下的负后向,就像这样:
I think in this case you want negative lookbehind, like so:
foo.*(?<!bar)
这篇关于正则表达式来匹配的东西,不是一个具体的子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!