【发布时间】:2011-11-19 12:44:09
【问题描述】:
我觉得问这个问题有点傻,但从我读过的所有内容来看,这应该有效,但对我来说却没有。我只是想使用正则表达式匹配字符串中的整个单词。
所以,如果我想在一个句子中找到单词“the”,它应该为“the quick brown fox jumps over the lazy dog”返回 true,但对于“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。我觉得我在这里遗漏了一些非常明显的东西,因为这应该不会太难。 :)
【问题讨论】:
-
感谢您的回答,尽管我现在觉得自己更愚蠢了。 :)