Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 6th, 2006, 9:20 PM   #1
larry
Newbie
 
Join Date: Jun 2006
Posts: 3
Rep Power: 0 larry is on a distinguished road
need pattern matching help

Hi! I am new to pattern matching and need some quick help. I want user to be able to enter in a string and have it match against another string.

if they enter "jo.n sm.th" I need it to match to "john smith"

I'm still learning the matching rules and am hoping you can help me with the above example so I can learn a little faster. Thank you. I appreciate your help.

Last edited by larry; Jun 6th, 2006 at 9:41 PM.
larry is offline   Reply With Quote
Old Jun 6th, 2006, 9:30 PM   #2
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
What you need is a 'wildcard'. In case you haven't heard of them, they do basicly exactly what you want. In your case it would take the '.' and match the rest of the letters, allowing the place where the period is too be any letter. Java has support for this by the looks of it. Try Google. I can't give ya much else on this, but you also might want to consider looking into Regular expressions.
Booooze is offline   Reply With Quote
Old Jun 6th, 2006, 9:34 PM   #3
larry
Newbie
 
Join Date: Jun 2006
Posts: 3
Rep Power: 0 larry is on a distinguished road
yeah I know it has a Pattern and Matcher class.

Pattern pat = Pattern.compile(find); //need to put pattern here
Matcher m = pat.matcher(tarray[2]);
if(m.find())

I'm trying to let the user enter in a name and have it match against tarray[2]. I'd prefer if the user could enter jo.n smi.h and have the matcher find it.

I'm hoping someone that knows regular expressions can help me out.
larry is offline   Reply With Quote
Old Jun 6th, 2006, 11:04 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Assuming you want a period in the input string to match any character in the string you are matching it against, the following code will suffice:

String input = "jo.n sm.th";
String match = "john smith";
boolean matches = match.matches(input);
System.out.println(matches);
Since a period (.) represents any single character in a regular expression, we can simply feed the input string into the String.matches(String regex) method and determine whether the result is true or false.

However, this method has obvious drawbacks; for example, the input string ".*" will match anything. I would suggest finding a way to escape any characters that could be used in regular expressions (except for periods) by prefixing them with a backslash, or if that's too complicated, simply stripping them out.

You can also use the Pattern/Matcher classes (in java.util.regex) if you prefer:

String input = "jo.n sm.th";
String match = "john smith";
Pattern compiledRegex = Pattern.compile(input);
Matcher regexMatcher = compiledRegex.matcher(match);
System.out.println(regexMatcher.matches());
I'm not an expert at using regular expressions, so there may be a better way to go about this.

Last edited by titaniumdecoy; Jun 6th, 2006 at 11:22 PM.
titaniumdecoy is offline   Reply With Quote
Old Jun 6th, 2006, 11:42 PM   #5
larry
Newbie
 
Join Date: Jun 2006
Posts: 3
Rep Power: 0 larry is on a distinguished road
I really appreciate your help. The code examples are helping me. Your example is very close and I think I am getting somewhere with it. Thank you.

I have a String named find that contains a name passed from the user. I need to compare it to a name in a file, which is tarray[2]. I am trying to find a way so the user can enter in let's say jo.n smi.h and it compares it to tarray[2]. If it doesn't match it goes to the next record. Maybe even john sm*.

My initial code just compared two strings, but that wasn't good enough because they had to match exactly.

I appreciate evreyone's help. Thank you.
larry is offline   Reply With Quote
Old Jun 7th, 2006, 12:11 AM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I would recommend first removing all non-alphanumeric characters (excepting "."), then replacing any occurences of "." with "*". That way any occurences of "." in the input text will match anything, so "jo.n sm.th" will match "john smith", "jonathan epsilon smoog tooth", etc. If that's not what you're looking for, you'll have to be more specific about exactly what you want to match.
titaniumdecoy is offline   Reply With Quote
Old Jun 7th, 2006, 12:23 PM   #7
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Small correction to my last post: "." should become ".*", not "*".
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:22 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC