正则表达式匹配的东西,如果没有其他东西 [英] regex for matching something if it is not preceded by something else

查看:92
本文介绍了正则表达式匹配的东西,如果没有其他东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,使用java中的regex,我想编写一个正则表达式,当且仅当模式前面没有某些字符时才匹配。例如:

So with regex in java, I want to write a regex that will match if and only if the pattern is not preceded by certain characters. For example:

String s = "foobar barbar beachbar crowbar bar ";

如果bar前面没有foo,我想匹配。因此输出将是:

I want to match if bar is not preceded by foo. So output would be:

barbar
beachbar
crowbar
bar

我知道这可能是一个非常简单的问题。我正在尝试学习正则表达式,但与此同时我需要一些工作。

I know this is probably a very simple question. I am trying to learn regex, but in the meantime I need something to work now.

推荐答案

你想要使用负面的背后隐藏像这样:

\w*(?<!foo)bar

其中(?<!x)表示只有在此点之前没有x。

Where (?<!x) means "only if it doesn't have "x" before this point".

参见正则表达式 - 外观以获取更多信息。

编辑:添加了 \w * 以捕获之前的字符(例如海滩)。

Edit: added the \w* to capture the characters before (e.g. "beach").

这篇关于正则表达式匹配的东西,如果没有其他东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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