Link to home
Start Free TrialLog in
Avatar of micro_learner
micro_learner

asked on

Java String wildcard Matching

Java Pattern Matching ...


I have a list of Strings which represent the names ..for eg:

[hellothere ,cellomello ...]


I have an existing function which filters based on the user input, basically to see if there is any substring


if(name.indexOf(matchString) >= 0)


Hence this will return true if the user input is hello or hell etc ...

But now I need to fileter when there  are any wildcards like % or $ etc ..

so that it returns true for inputs such as hel* or he*l
SOLUTION
Avatar of Ajay-Singh
Ajay-Singh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of micro_learner
micro_learner

ASKER

Not sure I understood it ...so is there a  '.' after hel

Could you please elaborate I am lost ..so just plug in the input string ?

Can you give me a snippet please.
>>String regex = "hellothere|cellomello|hel\\*|he\\*l|[%$]" ;
>>Pattern pattern = Pattern.compile( regex, Pattern.CASE_INSENSITIVE ) ;
..Matcher matcher = pattern.matcher( name ) ;

Can you Please give me a clearer example ( new to Java ...) where the input (with wild card) is searched against  a list of strings .

Appreciate your time.

Thanks
>>Can you Please give me a clearer example ( new to Java ...)

which part you don't understand?

>>where the input (with wild card) is searched against  a list of strings

hel\\*|he\\*l| - this is your input with wild cards!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i am not sure what I am doing wrong but I tried the above but could not get the match..I am nots ure what I am doing wrong...

Thanks for your time.



#################
import java.util.*;
import java.util.regex.*;
public class Test {
      public static void main(String[] args) {
            ArrayList input = new ArrayList();
            String matchString ="HEL\\*";
            input.add("HELLO");
            input.add("HELLOMELLO");
            input.add("HELLCELL");
            input.add("HELLOE");
            
            for (Iterator i = input.iterator(); i.hasNext(); ) {
                  String name = (String)i.next();
            Pattern pattern = Pattern.compile( name, Pattern.CASE_INSENSITIVE ) ;
             Matcher matcher = pattern.matcher( matchString ) ;
             if( matcher.find())
       System.out.println("Match Found for Input >> " + matchString + " for this >> " + name);
            else
       System.out.println("No Match Found for Input >> " + matchString + " for this >> " + name);
            }
}    
}
     
The below code sippet works ..but it works without the backspaces ...also it gives a runtime error if I have say ...'hel**'



#########################################
public static void main(String[] args) {
            // TODO Auto-generated method stub

            ArrayList input = new ArrayList();
            
            String matchString ="HE*";
            
            input.add("HELLO");
            input.add("HELLOMELLO");
            input.add("HELLCELL");
            input.add("HELLOE");
            
            for (Iterator i = input.iterator(); i.hasNext(); ) {
                  String name = (String)i.next();
                  /*if(name.indexOf(matchString.toUpperCase()) >= 0)
                  {
                        System.out.println("Match Found for Input >> " + matchString + " for this >> " + name);
                  }*/
                        
                  
                   //String regex = name ;
                   Pattern pattern = Pattern.compile( matchString, Pattern.CASE_INSENSITIVE ) ;
                   Matcher matcher = pattern.matcher( name ) ;

                   if( matcher.find())
                   {
                         System.out.println("Match Found for Input >> " + matchString + " for this >> " + name);
                   }
                   else
                   {
                         System.out.println("No Match Found for Input >> " + matchString + " for this >> " + name);
                   }
            
                  
            }