在这里,我们应该添加该逻辑无论字母,在这些数组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;
}
}
例如,我把字“电话”,但我的内容只是字“电话”。所以无论如何搜索应该找到它
超级,它的工作!谢谢! –