有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我几乎从过去几天就开始这样做了,但仍然无法获得所需的输出。 我有一个数组要说 wordlist[]={"One","Two","Three","Four","Five"}; 然后我从用户那里获取输入

String input="I have three no, four strings";

现在我要做的是对字符串执行搜索操作,以检查数组wordlist[]中可用的单词; 与上面的示例类似,输入字符串包含数组中的单词3和4。 因此,它应该能够从字符串中可用的数组中打印这些单词,如果wordlist[]中没有可用的单词,那么它应该打印“找不到匹配项”

这是我的密码,我很惊讶。 请

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
                }
            }
        }


    }
}

共 (3) 个答案

  1. # 1 楼答案

    您需要将matches与条件input.matches(".*\\b"+wordlist[i]+"\\b.*")一起使用

    .*:匹配任何内容

    \\b:避免将fourfourteen匹配的单词边界

    wordlist[i]是你的话

    1.)使用循环遍历数组

    (二)从数组中选取单词并对给定的正则表达式使用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
    
  2. # 2 楼答案

    使用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);
    
  3. # 3 楼答案

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