2017-02-06 9 views
1

で特定の単語を見つけるために、どのように私はAで始まるいくつかの3つの文字の単語を見つけると、式のこのタイプではEで終了したい:私のプログラムは辞書があるはトライ


A Eと私はこれを使用していますコード:

public ArrayList<String> Search(String word){ 

    Node current = root; 
    ArrayList<String> result = new ArrayList<>(); 

    while(current != null){ 
     String st = ""; 
     for(int i = 0; i < word.length(); i++){ 
      if(current.SubNode(word.charAt(i)) != null) { 
       current = current.SubNode(word.charAt(i)); 
       st+= current; 
      } 
      if(word.charAt(i) == '?'){ 
       current = current.SubNode(word.charAt(i)); 
       st+= current; 
      } 
     } 
     if (current.prefixes == true) 
      result.add(st); 
    } 
    return result; 
} 

しかし、それはここで

+1

なぜ正規表現を使用しないのですか?それはずっときれいだろう。 – Thibstars

+0

私は何をすべきかわかりません! – S206

答えて

0

を動作しませんが、それは正規表現を使用して見つける方法の例です。あなたのコレクションをちょう​​ど繰り返す。

String input = "AoE"; 

    Pattern p = Pattern.compile("(A*.*E)"); 
    Matcher m = p.matcher(input); 

    while (m.find()) { 
     System.out.println("m.length() = " + m.group().length()); 
     if (m.group().length() == 3){ 
      System.out.println("Found a " + m.group() + "."); 
     } 
    } 
関連する問題