1

I have been doing this since almost past few days but still unable to get a required output. Well I have an array say wordlist[]={"One","Two","Three","Four","Five"}; and then i take input from the user.

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

Now what i want to do is perform a search operation on the string to check for the words available in the array wordlist[]; Like in the above example input string contains the words three and four that are present in the array. so it should be able to print those words from array available in the string, and if no words from the wordlist[] are available then it should print "No Match Found".

Here's My code i'm struck with this. Please

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


    }
}
2
  • in the if condition put mat.matches(). In the if body increment a counting integer and use it outside the for loops to print "no match found" in case of zero matches. Also check that "\b"+ip[j]+"\b" regex, it doesn't work to me Commented Feb 22, 2017 at 13:58
  • 1
    @Oneiros matches add ^ and $ so it should be the complete match Commented Feb 22, 2017 at 14:29

3 Answers 3

5

You need to use matches with condition input.matches(".*\\b"+wordlist[i]+"\\b.*")

.* : match anything

\\b: word boundary to avoid matching four with fourteen

and wordlist[i] is your word

1.) Traverse your array using loop

2.) Pick words from array and use matches with given regex to avoid matching four with fourteen

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

Output :

three
Sign up to request clarification or add additional context in comments.

1 Comment

@IVANPAUL i am glad that i could help , happy coding
1

Using Java8 Streams you can do:

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

4 Comments

Hey thanks for this, it is showing various errors when i tried compilling your code. Which update of java 8 do i need to use to make the code error free I am currently using update 77. showing cannot find symbol errors on Collectors and Arrarys.
You have to import import java.util.stream.Collectors; and import java.util.Arrays; I'll change the code to reflect it.
Thanks fella. I'm completely new to streams. Thanks a lot
Remember to accept an answer. I think that of Pavneet Singh would make more sense because it is better explaining the regex, I wrote mine just to show a more compact way to do the same.
1

Here preparing a regex : \\b(one|two|three|four|five)\\b and checking the count of matcher.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.