![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2006
Posts: 3
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2006
Posts: 3
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Expert Programmer
|
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); 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()); Last edited by titaniumdecoy; Jun 6th, 2006 at 11:22 PM. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jun 2006
Posts: 3
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#6 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
Small correction to my last post: "." should become ".*", not "*".
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|