I have a Java program that does some String matching. I'm looking for anything that matches \d+x\d+ in a String. This works, using the Pattern and Matcher classes. However, to parse the String parts I have found, I have to manually parse the String I get from the Matcher.find() and Matcher.group(). How can I tell the Pattern I'm looking for something in the form of (\d+)x(\d+) and get the Matcher to return those groups separately?
So instead of the string "1x23" I want to get two strings, "1" and "23".
views:
156answers:
1
+3
A:
Use Matcher.group(int), not Matcher.group().
With the given regex and input, group(1) should be "1" and group(2) should be "23".
Michael Myers
2009-06-18 16:04:45
Note that you need your pattern to be the second one you listed: (\d+)x(\d+) so the matcher knows what are groups.
Kathy Van Stone
2009-06-18 16:17:52
Yes, I should have clarified that (since there are two regexes given).
Michael Myers
2009-06-18 16:22:35
Works like a charm, thx
Jorn
2009-06-19 09:33:38