2013-12-11 139 views
-1

在这里,我们应该添加该逻辑无论字母,在这些数组argi的url中,有一个特定的字...字符串“字” - 我们在搜索中引入的一个字。请帮助。下面是代码:如何添加一些搜索逻辑

public class Search { 

    private String word; 
    private String str=""; 

      public String getWord() { 
      return word; 
     } 

     public void setWord(String word) { 
      this.word = word; 
     } 
     public String getStr() { 
      return str; 
     } 

     public void setStr(String str) { 
      this.str = str; 
     } 

     private final Pattern TITLE = Pattern.compile("\\<title\\>(.*)\\<\\/title\\>"); 

     public String search(String url, String someword) { 

      try { 
       InputStreamReader in = new InputStreamReader(new URL(url).openStream(),"UTF-8"); 
       StringBuilder input = new StringBuilder(); 
       int ch; 
       while ((ch = in.read()) != -1) { 
        input.append((char) ch); 
       } 
       if (Pattern.compile(someword).matcher(input).find()) { 
        Matcher title = TITLE.matcher(input); 
        if (title.find()) { 
         return title.group(1); 
        } 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (PatternSyntaxException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
     public String toString() { 
      String[] argi = {"http://localhost:8080/site/endipnagradi", "http://localhost:8080/site/contacts_en", "http://localhost:8080/site/news_en"}; 

      for (int i = 0; i < argi.length; i++) { 
      String result = search(argi[i], word); 
      String regex = "^[А-Яа-я]+$"; 


      if (result != null && word.length()>2) { 

        str += "Search phrase " + "<b>"+ word + "</b>" + " have found in " + "<a href=\"" + argi[i] + "\">" + result + "</a>"+ "<p></p>"; 

        } 

      if(word.length()<3 || word.matches(regex)){ 

       str="Word not found!"; 
      } 

      if (word == null || word.isEmpty()) { 

       str = "Enter a search word!"; 

       } 
      } 
      return null; 
    } 
} 

例如,我把字“电话”,但我的内容只是字“电话”。所以无论如何搜索应该找到它

回答

0

用正确的选项创建你的模式,以便它会做一个不区分大小写的匹配。 从更改代码:

if (Pattern.compile(someword).matcher(input).find()) 

if (Pattern.compile(someword, Pattern.CASE_INSENSITIVE).matcher(input).find()) 

欲了解更多信息,看看here

+0

超级,它的工作!谢谢! –

1

你可以使用equalsIgnoreCase();。然后你可以有一个解决方案

"phone".equalsIgnoreCase("Phone") ; // this will return true and found the word 
+0

但我有可变的字符串单词。如何将它添加到我的代码? –