您可以使用带有单词边界字符的正则表达式:
String text = "an ancient manuscripts, another means to divide sentences into paragraphs was a line break (newline) followed by an initial at the beginning of the next paragraph. An initial is an oversize capital letter, sometimes outdented beyond the margin of text. This style can be seen, for example, in the original Old English manuscript of Beowulf. Outdenting is still used in English typography, though not commonly.[4] Modern English typography usually indicates a new paragraph by indenting the first line";
Matcher m = Pattern.compile("\\bparagraph\\b").matcher(text);
while (m.find()) {
System.out.println("Matching at: " + m.start());
}
如果你不想要“段落”。(“段落”后跟一个点),你可以试试
Matcher m = Pattern.compile("\\bparagraph($| )").matcher(text);
这意味着段落后跟空格或行尾。
如果您要查找的字符串可以包含特殊字符(如“(”),您可以使用Pattern.quote()
它来转义它:
String mySearchString = "paragraph";
Matcher m = Pattern.compile("\\b" + Pattern.quote(mySearchString) + "($| )").matcher(text);