Java正则表达式:匹配多个单词边界

时间:2015-05-12 15:50:22

标签: java regex

我想在文本中匹配几个单词。有以下内容:

if ( Pattern.matches(".*\\b" + placeSub.toLowerCase() + "\\b" + placeSup.toLowerCase() + "\\b.*", sourceText.toLowerCase()) ) {
    System.out.println( String.format("Matched %s on %s", placeSub, placeSup) );        
}

变量placeSubplaceSup& 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

示例输入:

  1. placeSub:South

  2. placeSup:Sudan

  3. sourceText:tens of thousands of people have fled fierce fighting in south sudan's northern unity state

1 个答案:

答案 0 :(得分:2)

你应该实际使用:

Pattern.matches(
   ".*?\\b" + placeSub.toLowerCase() + "\\b\\W+\\b" + placeSup.toLowerCase() + "\\b.*",
   sourceText.toLowerCase())

将转化为:

/.*?\bsouth\b\W+\bsudan\b.*/i

See regex demo here