Results 1 to 2 of 2
- 10-27-2009, 11:11 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 31
- Rep Power
- 0
Will someone pliz help me with this code on pattern matching
Java Code://In the code below, the matched string must be in the form XDD/DDDD/DDDD //or XDDm/DDDD/DDDD where //X must be a upper-case-letter from A through Z //D can be any number from 0 through 9. //and m can be any lower-case-letter from a through z. //**************** //the method getMatchedString should return the matchString if and only //matchedString matches either XDD/DDDD/DDDD or XDDm/DDDD/DDDD //the delimiter "/" is inclusive of the pattern. public String getMatchedString() { System.out.println("Please reply with a string: "); Pattern p = Pattern.compile("^[A-Z]\\d{2}/\\d{4}/\\d{4}$"); Pattern k = Pattern.compile("^[A-Z]\\d{2}[a-z]{1}/\\d{4}/\\d{4}$"); String matchedString = input.next(); boolean match = true; boolean firstMatch = p.matcher(matchedString).find(); boolean secondMatch = k.matcher(matchedString).find(); while(firstMatch != match || secondMatch != match ){ System.out.println("Invalid string!Try again."); matchedString = input.next(); } return matchedString; } //thanks in advanceLast edited by nmvictor; 10-27-2009 at 11:23 AM.
- 10-27-2009, 07:33 PM #2
It seems your not familial with regex syntax, since you can easily combine the two patterns using a "?" to make the optional part optional (code below). Check out regular-expressions.info, which has an excellent regex tutorial.
In your example, the second pattern has the lower-case letter as optional. To make something optional add a "?" afterwards. This is the same as adding the repetition quantifier {0,1}.
Java Code:String regex = "^[A-Z]\\d{2}[COLOR="Red"][a-z]?[/COLOR]/\\d{4}/\\d{4}$"
As for what you want to do with the regex, it's unclear to me. It seems that you want to check if the input matches the pattern. If it doesn't, you ask for a new input. However, you don't reset your matcher with the new input, nor do you try to perform a match with the new input, so if it's false the first time, you'll get an infinite loop.
I think the below code is similar to what you want, but not sure (looking for feedback).
Java Code:// used to simulate user input String[] inputs = {"!", "-", "a word", "W"}; int index = 0; String regex = "\\w"; Pattern pattern = Pattern.compile(regex); String matchedString = inputs[index++]; System.out.println("Trying: " + matchedString); Matcher matcher = pattern.matcher(matchedString); while (!matcher.matches()) { matchedString = inputs[index++]; System.out.println("Input failed, trying: " + matchedString); //try with next input matcher.reset(matchedString); } System.out.println("Matched: " + matchedString);CodesAway - codesaway.info
writing tools that make writing code a little easier
Similar Threads
-
Re-usable, dynamic pattern for error code-message mapping
By Algatron in forum Advanced JavaReplies: 3Last Post: 03-19-2009, 04:01 AM -
How to Print out adiamond pattern I try this code but
By masaka in forum New To JavaReplies: 11Last Post: 04-16-2008, 01:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks