无法匹配单词的一部分

时间:2014-12-09 06:38:40

标签: java regex

我写了一个程序来复制第一行,其中包含匹配的单词,单词或部分单词。我使用以下代码得到了单词或单词的结果。当我输入单词的一部分时,我得到空值。

public static void main(String[] args) {

        String paragraph="First time in a long time the cricket team is in Kandy and there is no rain and the skies are blue. Strange days. Hope it stays that way";
        String searchWord="lo";
        Pattern p = Pattern.compile("([A-Z][^.?!]*?)?(?<!\\w)(?i)(" + searchWord + ")(?!\\w)[^.?!]*?[.?!]{1,2}\"?");
        Matcher m = p.matcher(paragraph);
        String sentence = null;
        while (m.find()) {
              sentence = m.group();
              break;
        }
        System.out.println(sentence);
  }

请建议我如何搜索部分内容?

2 个答案:

答案 0 :(得分:1)

是的,你必须得到一个空匹配作为输出,因为子串lo之后是字符串n中的单词字符long。因此,请在正则表达式中将否定前瞻(?!\\w)更改为正向前瞻(?=\\w)以获得匹配。

Pattern p = Pattern.compile("([A-Z][^.?!]*?)?(?<!\\w)(?i)(" + searchWord + ")(?=\\w)[^.?!]*?[.?!]{1,2}\"?");

DEMO

String paragraph="First time in a long time the cricket team is in Kandy and there is no rain and the skies are blue. Strange days. Hope it stays that way";
String searchWord="lo";
Pattern p = Pattern.compile("([A-Z][^.?!]*?)?(?<!\\w)(?i)(" + searchWord + ")(?=\\w)[^.?!]*?[.?!]{1,2}\"?");
Matcher m = p.matcher(paragraph);
String sentence = null;
while (m.find()) {
      sentence = m.group();
      break;
}
System.out.println(sentence);

<强>输出:

First time in a long time the cricket team is in Kandy and there is no rain and the skies are blue.

答案 1 :(得分:0)

在这种情况下,您不需要正则表达式。您可以使用String.split()方法简化操作。

根据点进行分割,找到每个句子中的字符,以获得所需的句子。

示例代码:

String paragraph = "First time in a long time the cricket team is in Kandy and there is no rain and the skies are blue. Strange days. Hope it stays that way";
String searchWord = "lo";
String sentence = null;
for (String str : paragraph.split("\\.\\s*")) { 
    // split based on dot followed by zero or more whitespaces
    if (str.indexOf(searchWord) != -1) { 
        // or use if(str.contains(searchWord))
        sentence = str;
        break;
    }
}
System.out.println(sentence);