2014-06-25 35 views
-5

我有一些內容,我試圖找出內容中是否存在一些「searchText」。當我在「searchText」中查找單個單詞時,String.contains(searchText)返回true,但對於多個工作返回false。下面是顯示實現的代碼片段。我需要確定searchText是否存在於內容中。String.contains(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上。

+2

由於字符串中包含「特定文本」,所以* *將返回true。換句話說,你已經舉了一個例子,它已經可以工作 - 請給我們一個例子,你會希望*工作,但沒有。 –

+0

這是行得通的。另外,found = input.contains(searchText) – JesusS

+0

你的代碼返回true! –

回答

0

如上所述,文本是多行的。第一次出現\ n之後的任何內容都找不到。我用空格替換了\ n,它工作正常。

相關問題