Java Regex:将整个单词与单词边界匹配

时间:2022-09-19 18:54:15

I am trying to check whether a string contains a word as a whole, using Java. Below are some examples:

我正在尝试使用Java检查字符串是否包含整个单词。以下是一些例子:

Text : "A quick brown fox"
Words:
"qui" - false
"quick" - true
"quick brown" - true
"ox" - false
"A" - true

Below is my code:

以下是我的代码:

String pattern = "\\b(<word>)\\b";
String s = "ox";
String text = "A quick brown fox".toLowerCase();
System.out.println(Pattern.compile(pattern.replaceAll("<word>", s.toLowerCase())).matcher(text).find());

It works fine with strings like the one I mentioned in the above example. However, I get incorrect results if the input string has characters like %, ( etc, e.g.:

它与我在上面的例子中提到的字符串一样正常工作。但是,如果输入字符串包含%,(等,例如:

Text : "c14, 50%; something (in) bracket"
Words:
"c14, 50%;" : false
"(in) bracket" : false

It has something to do with my regex pattern (or maybe I am doing the entire pattern matching wrongly). Could anyone suggest me a better approach.

它与我的正则表达式模式有关(或者我可能错误地进行了整个模式匹配)。谁能建议我一个更好的方法。

2 个解决方案

#1


5  

It appears you only want to match "words" enclosed with whitespace (or at the start/end of strings).

看起来你只想匹配用空格(或在字符串的开头/结尾)附带的“单词”。

Use

String pattern = "(?<!\\S)" + Pattern.quote(word) + "(?!\\S)";

The (?<!\S) negative lookbehind will fail all matches that are immediately preceded with a char other than a whitespace and (?!\s) is a negative lookahead that will fail all matches that are immediately followed with a char other than whitespace. Pattern.quote() is necessary to escape special chars that need to be treated as literal chars in the regex pattern.

(?

#2


0  

Try escape the special characters with the backslash. They can have other meanings in a pattern.

尝试使用反斜杠转义特殊字符。它们可以在一种模式中具有其他含义。

small correction: Probably you even need two backslash, since the backslash itself is a special character in a String.

小修正:可能你甚至需要两个反斜杠,因为反斜杠本身是String中的一个特殊字符。

#1


5  

It appears you only want to match "words" enclosed with whitespace (or at the start/end of strings).

看起来你只想匹配用空格(或在字符串的开头/结尾)附带的“单词”。

Use

String pattern = "(?<!\\S)" + Pattern.quote(word) + "(?!\\S)";

The (?<!\S) negative lookbehind will fail all matches that are immediately preceded with a char other than a whitespace and (?!\s) is a negative lookahead that will fail all matches that are immediately followed with a char other than whitespace. Pattern.quote() is necessary to escape special chars that need to be treated as literal chars in the regex pattern.

(?

#2


0  

Try escape the special characters with the backslash. They can have other meanings in a pattern.

尝试使用反斜杠转义特殊字符。它们可以在一种模式中具有其他含义。

small correction: Probably you even need two backslash, since the backslash itself is a special character in a String.

小修正:可能你甚至需要两个反斜杠,因为反斜杠本身是String中的一个特殊字符。