2017-02-22 84 views
1

自近幾天來我一直在做這件事,但仍然無法獲得所需的輸出。 那麼我有一個數組說 wordlist[]={"One","Two","Three","Four","Five"}; 然後我從用戶的輸入。在輸入字符串中搜索數組中的關鍵字並打印它們

String input="I have three no, four strings"; 

現在我想做的事是要檢查的字符串在單詞表數組[]提供的詞進行搜索操作; 就像在上面的例子中,輸入字符串包含數組中存在的三個和四個字。 所以它應該能夠打印字符串中可用數組中的那些單詞,並且如果沒有來自單詞表[]的單詞可用,那麼它應該打印「找不到匹配單詞」。

這是我的代碼,我很震驚。 請

import java.util.regex.*; 
import java.io.*; 
class StringSearch{ 
    public static void main(String ...v)throws IOException{ 
     BufferedReader cin = new BufferedReader(new InputStreamReader(System.in)); 
     String wordlist[]={"one","two","three","four","five"}; 
     String input=cin.readLine(); 
     int i,j; 
     boolean found; 
     Pattern pat; 
     Matcher mat; 
     Pattern spliter=Pattern.compile("[ ,.!]"); 
     String ip[]=spliter.split(input); 
     System.out.println(ip[2]); 
     for(i=0; i<wordlist.length;i++){ 
      for(j=0;j<ip.length;j++){ 
       pat=Pattern.compile("\b"+ip[j]+"\b"); 
       mat=pat.matcher(wordlist[i]); 
       if(){ 
         // No Idea What to write here 
       } 
      } 
     } 


    } 
} 
+0

在if條件中放了'mat.matches()'。在if體內增加一個計數整數,並在for循環之外使用它來打印「找不到匹配」,以防零匹配。也檢查''\ b「+ ip [j] +」\ b「'正則表達式,它對我不起作用 – Oneiros

+1

@Oneiros'匹配'添加'^'和'$'所以它應該是完全匹配 –

回答

5

您需要使用matches與條件input.matches(".*\\b"+wordlist[i]+"\\b.*")

.*匹配任何

\\b:單詞邊界,以避免與fourteen

wordlist[i]匹配four是你的話

1.)使用環

2.遍歷您的陣列)從陣列接字,並使用matches具有給定正則表達式來避免與fourteen

String wordlist[]={"one","two","three","four","five"}; 
    String input="I have three no, fourteen strings"; 
    int i; 
    boolean found=false; 
    // Traverse your array 
    for(i=0; i<wordlist.length;i++){ 
      // match your regex containing words from array against input 
      if(input.matches(".*\\b"+wordlist[i]+"\\b.*")){ 
       // set found = true 
       found=true; 
       // display found matches 
       System.out.println(wordlist[i]); 
      } 
     } 
    // if found is false here then mean there was no match 
    if (!found) { 
     System.out.println("No Match Found"); 
    } 

輸出匹配four

three 
+0

**非常感謝你的幫助..它爲我工作.... –

+0

@IVANPAUL我很高興我可以幫助,快樂的編碼 –

1

使用您可以執行Java8 Streams:

... 
import java.util.Arrays; 
import java.util.stream.Collectors; 
... 

String wordlist[]={"one","two","three","four","five"}; 
String input=cin.readLine(); 
String foundStrings = 
     Arrays.stream(wordlist) 
     .filter(s->input.matches(".*\\b"+s+"\\b.*")) 
     .collect(Collectors.joining("\n")); 

System.out.print(foundStrings.isEmpty() ? "No Match Found\n": foundStrings); 
+0

嘿謝謝你,這是顯示各種錯誤,當我嘗試compilling你的代碼。我需要使用哪個更新的Java 8來使代碼無錯誤我目前正在使用更新77.在**收藏家**和** Arrarys **上顯示無法找到符號錯誤。 –

+0

您必須導入'import java.util.stream.Collectors;'和 'import java.util.Arrays;'我將更改代碼以反映它。 – Teudimundo

+0

謝謝小夥子。我完全不熟悉流。非常感謝 –

0

這裏準備一個正則表達式:\\ b(one | two | three | four | five)\\ b並檢查匹配器的計數。

String wordlist[]={"one","two","three","four","five"}; 
String input="I have three no, fourteen strings"; 

StringBuilder regexBuilder = new StringBuilder("\\b").append("(").append(String.join("|", wordlist)).append(")").append("\\b"); 
String regexExp = regexBuilder.toString(); 
regexBuilder.setLength(0); 

Pattern p = Pattern.compile(regexExp); 
Matcher matcher = p.matcher(input); 

int count = 0; 
while (matcher.find()) 
{ 
     System.out.println(matcher.group()); 
     count++; 
} 

if(count == 0){ 
    System.out.println("No Match Found"); 
} 
相關問題