从输入字符串中的数组中搜索关键字并打印它们

Search a keyword from an array in an input string and Print them

几乎过去几天我一直在这样做,但仍然无法获得所需的输出。 好吧,我有一个数组说 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
                }
            }
        }


    }
}

您需要在条件 input.matches(".*\b"+wordlist[i]+"\b.*")

下使用 matches

.* : 匹配任何东西

\b:单词边界以避免将 fourfourteen

匹配

wordlist[i]是你的话

1.) 使用循环遍历数组

2.) 从数组中挑选单词并使用 matches 和给定的正则表达式以避免将 fourfourteen

匹配
    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");
    }

输出:

three

使用 Java8 流,您可以执行以下操作:

 ...
 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);

这里准备一个正则表达式:\\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");
}