我已将文本文件的内容存储在一个名为的字符串Str
中,如下所示:
Str = "Cat is an animal\n" +
"Cat is small\n" +
"Cat is a pet\n";
我写了这段代码来搜索这个词Cat
:
Pattern pattern = Pattern.compile("(\\bCat\\b)");
Matcher match = pattern.matcher(Str);
while (match.find()) {
String tempStr = "I found " + match.group() + "\n";
}
以上产生了这个输出:
I found Cat
I found Cat
I found Cat
这是我的问题。如何使用关键字 Cat 找到整个句子,以便输出为:
I found Cat is an animal
I found Cat is small
I found Cat is a pet
什么是正则表达式?