views:

156

answers:

1

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".

+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
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
Yes, I should have clarified that (since there are two regexes given).
Michael Myers
Works like a charm, thx
Jorn