【问题标题】:Search a keyword from an array in an input string and Print them从输入字符串中的数组中搜索关键字并打印它们
【发布时间】: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


【解决方案1】:

你需要使用matches和条件input.matches(".*\\b"+wordlist[i]+"\\b.*")

.* : 匹配任何东西

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

【讨论】:

  • @IVANPAUL 我很高兴能帮上忙,编码愉快
【解决方案2】:

使用 Java8 Streams 你可以做到:

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

【讨论】:

  • 您好,谢谢您,当我尝试编译您的代码时,它显示了各种错误。我需要使用 java 8 的哪个更新来使代码无错误我目前正在使用更新 77。显示在 CollectorsArrarys 上找不到符号错误。
  • 你必须导入 import java.util.stream.Collectors;import java.util.Arrays; 我会更改代码以反映它。
  • 谢谢小伙子。我对流完全陌生。非常感谢
  • 记得接受答案。我认为 Pavneet Singh 会更有意义,因为它更好地解释了正则表达式,我写我的只是为了展示一种更紧凑的方式来做同样的事情。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多