【发布时间】:2017-07-12 14:30:45
【问题描述】:
几乎过去几天我一直在这样做,但仍然无法获得所需的输出。
好吧,我有一个数组说
wordlist[]={"One","Two","Three","Four","Five"};
然后我接受用户的输入。
String input="I have three no, four strings";
现在我要做的是对字符串执行搜索操作以检查数组 wordlist[] 中可用的单词; 就像在上面的示例中,输入字符串包含数组中存在的单词 3 和 4。 所以它应该能够打印字符串中可用数组中的那些单词,如果 wordlist[] 中没有可用的单词,那么它应该打印“No Match Found”。
这是我的代码,我对此感到震惊。 请
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
}
}
}
}
}
【问题讨论】:
-
在 if 条件中输入
mat.matches()。在 if 正文中增加一个计数整数并在 for 循环之外使用它以在零匹配的情况下打印“未找到匹配”。还要检查"\b"+ip[j]+"\b"正则表达式,它对我不起作用 -
@Oneiros
matches添加^和$所以应该是完全匹配
标签: java regex string search string-search