我想在文本中匹配几个单词。有以下内容:
if ( Pattern.matches(".*\\b" + placeSub.toLowerCase() + "\\b" + placeSup.toLowerCase() + "\\b.*", sourceText.toLowerCase()) ) {
System.out.println( String.format("Matched %s on %s", placeSub, placeSup) );
}
变量placeSub
,placeSup
& sourceText
是动态的(运行时)。
上面的代码不起作用(不匹配)。但是,以下匹配:
if ( Pattern.matches(".*\\b" + placeSub.toLowerCase() + "\\s" + placeSup.toLowerCase() + "\\b.*", sourceText.toLowerCase()) ) {
System.out.println( String.format("Matched %s on %s", placeSub, placeSup) );
}
为什么文字能够匹配\\s
而不是\\b
?
示例输入:
placeSub:South
placeSup:Sudan
sourceText:tens of thousands of people have fled fierce fighting in south sudan's northern unity state
答案 0 :(得分:2)
你应该实际使用:
Pattern.matches(
".*?\\b" + placeSub.toLowerCase() + "\\b\\W+\\b" + placeSup.toLowerCase() + "\\b.*",
sourceText.toLowerCase())
将转化为:
/.*?\bsouth\b\W+\bsudan\b.*/i