如何匹配带有括号的模式?

发布于 2024-10-19 04:56:03 字数 412 浏览 5 评论 0原文

我试图在大文本中查找特定短语,但该短语可能包含“[”、“(”、“*”等字符,...如“name1 (name2”),但它查找它时会导致无效异常。这是我的代码:

Pattern myPattern = Pattern.compile( "\\b" + phrase + "\\b" );  // Exception
Matcher myMatcher = myPattern.matcher( largeText );

我尝试使用 quote(...) 来修复此类字符,但它不起作用:

phrase = Pattern.quote( phrase );

如何修复此问题以允许此类字符?< /强>

I am trying to look for specific phrase inside large text, but the phrase may contain characters like "[", "(", "*", ... like "name1 (name2", but it causes an invalid exception when looking for it. Here is my code :

Pattern myPattern = Pattern.compile( "\\b" + phrase + "\\b" );  // Exception
Matcher myMatcher = myPattern.matcher( largeText );

I have tried to use quote(...) to fix such characters but it didn't work :

phrase = Pattern.quote( phrase );

How can i fix this to allow such characters ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉宸 2024-10-26 04:56:03

Pattern.quote(phrase) 工作得很好:

String largeText = "a()b a()c a()b";
String phrase = "a()b";
Pattern myPattern = Pattern.compile( "\\b" + Pattern.quote(phrase) + "\\b" );
Matcher myMatcher = myPattern.matcher( largeText );
while(myMatcher.find()) {
  System.out.println(myMatcher.group());
}

打印:

a()b
a()b

Pattern.quote(phrase) works just fine:

String largeText = "a()b a()c a()b";
String phrase = "a()b";
Pattern myPattern = Pattern.compile( "\\b" + Pattern.quote(phrase) + "\\b" );
Matcher myMatcher = myPattern.matcher( largeText );
while(myMatcher.find()) {
  System.out.println(myMatcher.group());
}

prints:

a()b
a()b
恏ㄋ傷疤忘ㄋ疼 2024-10-26 04:56:03

处理短语以转义所有可能的正则表达式元字符。

Process phrase to escape all possible regex metacharacters.

不寐倦长更 2024-10-26 04:56:03

您能否提供一个重现此问题的完整示例?我已经尝试了以下方法,效果很好:

String largeText = "large text with name1 (name2) and possibly something more";
String phrase = "name1 (name2";
phrase = Pattern.quote( phrase );
Pattern myPattern = Pattern.compile( "\\b" + phrase + "\\b" );  // Exception
System.out.println("The pattern is " + myPattern.pattern());
Matcher myMatcher = myPattern.matcher( largeText );
if (myMatcher.find()) {
  System.out.println("A match is found: " + myMatcher.group());
}

输出是:

The pattern is \b\Qname1 (name2\E\b
A match is found: name1 (name2

Could you please provide a complete example that reproduces this problem? I've tried the following and it works fine:

String largeText = "large text with name1 (name2) and possibly something more";
String phrase = "name1 (name2";
phrase = Pattern.quote( phrase );
Pattern myPattern = Pattern.compile( "\\b" + phrase + "\\b" );  // Exception
System.out.println("The pattern is " + myPattern.pattern());
Matcher myMatcher = myPattern.matcher( largeText );
if (myMatcher.find()) {
  System.out.println("A match is found: " + myMatcher.group());
}

The output is:

The pattern is \b\Qname1 (name2\E\b
A match is found: name1 (name2
满地尘埃落定 2024-10-26 04:56:03

您可能只想使用:

int offset = largeText.indexOf(phrase);

来测试子字符串的存在/偏移量。

要使用模式,这应该可行:

String longString = "this[that]the other* things";
String phrase = "[that]";
Pattern myPattern = Pattern.compile( "\\b" + Pattern.quote(phrase) + "\\b"));
Matcher m = myPattern.matcher(longString);
if (m.find()) {
  System.out.println(m.group());
}

但是使用 * 和 ? 时有一个小问题。在短语的开头或结尾。

这些字符被视为空白字符(不是单词字符),因此如果它们出现在短语的开头或结尾,则为了匹配边界,它们必须包含所有前导/尾随空白。

如果短语的开头或结尾有这些字符,您可能需要通过删除“\b”来特殊处理。

You may want to just use:

int offset = largeText.indexOf(phrase);

to test the existence/offset of a substring.

To use patterns, this should work:

String longString = "this[that]the other* things";
String phrase = "[that]";
Pattern myPattern = Pattern.compile( "\\b" + Pattern.quote(phrase) + "\\b"));
Matcher m = myPattern.matcher(longString);
if (m.find()) {
  System.out.println(m.group());
}

But there's a little problem when using * and ? at the start or end of the phrase.

Those characters are treated like white space characters (not word characters) so if they appear at the beginning or end of a phrase, then to match the boundary they must include all the leading/trailing whitespace.

You may need to special case this by dropping the "\b" if the phrase has those characters at the start or end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文