因此,对于java中的regex,我想编写一个正则表达式,当且仅当模式前面没有某些字符时才会匹配 . 例如:
String s = "foobar barbar beachbar crowbar bar ";
如果bar之前没有foo,我想匹配 . 所以输出将是:
barbar beachbar crowbar bar
我知道这可能是一个非常简单的问题 . 我正在努力学习正则表达式,但与此同时我需要一些工作 .
你想像这样使用 negative lookbehind :
negative lookbehind
\w*(?<!foo)bar
(?<!x) 的意思是"only if it doesn't have " x " before this point" .
(?<!x)
有关更多信息,请参见Regular Expressions - Lookaround .
Edit :添加 \w* 以捕获之前的字符(例如"beach") .
\w*
1 回答
你想像这样使用
negative lookbehind
:(?<!x)
的意思是"only if it doesn't have " x " before this point" .有关更多信息,请参见Regular Expressions - Lookaround .
Edit :添加
\w*
以捕获之前的字符(例如"beach") .