String.contains(searchText)不适用于多个单词

时间:2014-06-25 07:25:17

标签: java android regex pattern-matching string-matching

我有一些内容,我试图找到内容中是否存在某些“searchText”。当我在“searchText”中查找单个单词时,String.contains(searchText)返回true但对多个工作返回false。以下是显示实现的代码段。我需要确定searchText是否存在于内容中。

    boolean found;
    String searchText = "particular text";
    String input = "This is the text area where I am trying " +
         "to look for the particular text, which is in the variable searchText. " +
         "This text will have the string (222M) as part of this string. " +
         "The find method should give me a true result even if I don't " +
         "enter the closing brakect of the word. This is a multiline string";
    if(input.contains(searchText)) {
        found = true;
    }

我需要使用正则表达式吗? 让我指出我之前未提及的研究和尝试。

我已经尝试了以下正则表达式

pattern = Pattern.compile("(?<=\\W)" + searchText + "(?=\\W)");

这也不起作用。

注意:这是在Android上。

1 个答案:

答案 0 :(得分:0)

如上所述,该文本是多行的。无法找到第一次出现\ n后的任何内容。我用空格替换了\ n并且它有效。