我觉得问这个问题有点傻,但从我读过的所有内容来看,这应该是可行的,但对我来说并不是。我只是想使用正则表达式匹配字符串中的整个单词。
所以,如果我想在句子中查找单词“the”,它应该返回“the quick brown fox jumps over the lazy dog”的真实值,但对于“there quick brown fox jumps over the lazy dog”的值应为false。
我已经尝试过以下方法:
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。我觉得我可能漏掉了一些很明显的东西,因为这应该不太困难。:)