Java正则表达式:否定前瞻 [英] Java regex: Negative lookahead

查看:122
本文介绍了Java正则表达式:否定前瞻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作两个匹配URI的正则表达式。这些URI的格式为: / foo / someVariableData / foo / someVariableData / bar / someOtherVariableData

I'm trying to craft two regular expressions that will match URIs. These URIs are of the format: /foo/someVariableData and /foo/someVariableData/bar/someOtherVariableData

我需要两个正则表达式。每个都需要匹配一个而不是另一个。

I need two regexes. Each needs to match one but not the other.

我最初提出的正则表达式是:
/ foo /.+ / foo /.+/ bar /.+

The regexes I originally came up with are: /foo/.+ and /foo/.+/bar/.+ respectively.

我认为第二个正则表达式很好。它只匹配第二个字符串。然而,第一个正则表达式匹配两者。所以,我开始玩(第一次)负向前瞻。我设计了正则表达式 / foo /.+(?!bar)并设置以下代码进行测试

I think the second regex is fine. It will only match the second string. The first regex, however, matches both. So, I started playing around (for the first time) with negative lookahead. I designed the regex /foo/.+(?!bar) and set up the following code to test it

public static void main(String[] args) {
    String shouldWork = "/foo/abc123doremi";
    String shouldntWork = "/foo/abc123doremi/bar/def456fasola";
    String regex = "/foo/.+(?!bar)";
    System.out.println("ShouldWork: " + shouldWork.matches(regex));
    System.out.println("ShouldntWork: " + shouldntWork.matches(regex));
}

当然,两者都解析为

And, of course, both of them resolve to true.

有谁知道我做错了什么?我不需要使用Negative lookahead,我只需要解决问题,我认为负面的预测可能是一种方法。

Anybody know what I'm doing wrong? I don't need to use Negative lookahead necessarily, I just need to solve the problem, and I think that negative lookahead might be one way to do it.

谢谢,

推荐答案

尝试

String regex = "/foo/(?!.*bar).+";

或可能

String regex = "/foo/(?!.*\\bbar\\b).+";

以避免路径失败,例如 / foo / baz / crowbars 我认为你确实希望正则表达式匹配。

to avoid failures on paths like /foo/baz/crowbars which I assume you do want that regex to match.

说明:(没有Java字符串所需的双反斜杠)

Explanation: (without the double backslashes required by Java strings)

/foo/ # Match "/foo/"
(?!   # Assert that it's impossible to match the following regex here:
 .*   #   any number of characters
 \b   #   followed by a word boundary
 bar  #   followed by "bar"
 \b   #   followed by a word boundary.
)     # End of lookahead assertion
.+    # Match one or more characters

\b ,单词边界锚,匹配字母数字字符和非字母数字字符之间的空格(或字符串的开头/结尾之间)和一个alnum角色)。因此,它在 b 之前或在bar中的 r 之后匹配,但在crowbar中 w b 之间无法匹配

\b, the "word boundary anchor", matches the empty space between an alphanumeric character and a non-alphanumeric character (or between the start/end of the string and an alnum character). Therefore, it matches before the b or after the r in "bar", but it fails to match between w and b in "crowbar".

Protip:看看 http:/ /www.regular-expressions.info - 一个很棒的正则表达式教程。

Protip: Take a look at http://www.regular-expressions.info - a great regex tutorial.

这篇关于Java正则表达式:否定前瞻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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