<分区>
我觉得问这个问题有点傻,但从我读过的所有内容来看,这对我来说应该有用,但没有用。我只是想使用正则表达式匹配字符串中的整个单词。
因此,如果我试图在句子中找到单词“the”,它应该为“the quick brown fox jumps over the lazy dog”返回 true,但为“there quick brown fox jumps over the懒狗”。
我已经试过了:
String text = "the quick brown fox jumps over the lazy dog";
return text.matches("\\bthe\\b");
我也试过:
String text = "the quick brown fox jumps over the lazy dog";
String regex = "\\bthe\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
return matcher.matches();
我也试过这个正则表达式:“\bthe\b”
而且它们总是返回 false。我觉得我在这里遗漏了一些非常明显的东西,因为这应该不会太难。 :)