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(matchStrin g) >= 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
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(matchStrin
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>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
>>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!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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);
}
}
}
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);
}
}
}
ASKER
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(matchStr
{
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);
}
}
ASKER
Could you please elaborate I am lost ..so just plug in the input string ?
Can you give me a snippet please.