Java 正则表达式模式与模式匹配,但其中有一些字符串

Java 正则表达式模式与模式匹配,但其中有一些字符串,java,regex,Java,Regex,我正试着为一些Pattern编写正则表达式 /web/surfer* --Match /web/surfer/information* --Not Match 有人能告诉我们如何在Java中编写正则表达式模式来匹配这一点吗?我相信您正在寻找类似正则表达式的负前瞻性: /web/surfer(?!/information).* 我相信你正在寻找像这个正则表达式一样的消极前瞻: /web/surfer(?!/information).* 试试这个例子: String s = "/web/sur

我正试着为一些Pattern编写正则表达式

/web/surfer* --Match
/web/surfer/information* --Not Match

有人能告诉我们如何在Java中编写正则表达式模式来匹配这一点吗?

我相信您正在寻找类似正则表达式的负前瞻性:

/web/surfer(?!/information).*

我相信你正在寻找像这个正则表达式一样的消极前瞻:

/web/surfer(?!/information).*
试试这个例子:

String s = "/web/surfer/information";
System.out.println(s.matches("/web/surfer(?!/information).*"));
试试这个例子:

String s = "/web/surfer/information";
System.out.println(s.matches("/web/surfer(?!/information).*"));
您可以尝试以下方法:

^/web/surfer(?>[^i]++|\\Bi|i(?!nformation\\b))*$
您可以尝试以下方法:

^/web/surfer(?>[^i]++|\\Bi|i(?!nformation\\b))*$

我认为混淆之处在于,您使用*匹配任何文本,而实际上您需要一个点

这有帮助吗

    String regex = "/web/surfer.*";
    System.out.println("/web/surfer*".matches(regex));  // prints true
    System.out.println("/web/surfer/information*".matches(regex)); // prints true

我认为混淆之处在于,您使用*匹配任何文本,而实际上您需要一个点

这有帮助吗

    String regex = "/web/surfer.*";
    System.out.println("/web/surfer*".matches(regex));  // prints true
    System.out.println("/web/surfer/information*".matches(regex)); // prints true

@卡西米菊酯。这里*表示任何文本。为什么不匹配?因为第三个/或者因为单词信息?还是由于长度的原因?@casimirithippolyte.*指任何文本。它可能包含也可能不包含/@casimirithippolyte。这里*表示任何文本。为什么不匹配?因为第三个/或者因为单词信息?还是由于长度的原因?@casimirithippolyte.*指任何文本。它可能包含也可能不包含/