Reputation: 175
Regex problem: I want to get groups twice, but don't know how to solve it.
Here is code:
public static void multiGroupTest() {
// Pattern p = Pattern.compile("(\\w+)(\\d\\d)(\\w+)");
Pattern p = Pattern.compile("([A-Z]{1})(\\d+)([A-Za-z]+)");
String str = "X89SuperJavaJavaX89SuperJava";
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
}
}
OK, the result is:
X
89
SuperJavaJavaX
What i want to get is:
X
89
SuperJavaJava
X
89
SuperJava
How can I separate the two matches?
Upvotes: 3
Views: 58
Reputation: 174706
Because the last group ([A-Za-z]+)
would greedily match the following X , you didn't able to get two strings. Use ((?:[A-Z][a-z]+)+)
to capture the words which are in this FooBar
format. Because names wouldn't end with a capital letter.
([A-Z])(\\d+)((?:[A-Z][a-z]+)+)
Pattern p = Pattern.compile("([A-Z])(\\d+)((?:[A-Z][a-z]+)+)");
String str = "X89SuperJavaJavaX89SuperJava";
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
}
Output:
X
89
SuperJavaJava
X
89
SuperJava
Upvotes: 1
Reputation: 67968
([A-Z]{1})(\d+)((?:(?!\1)[a-zA-Z])+)
Try this.See demo.
http://regex101.com/r/sU3fA2/59
Upvotes: 0
Reputation: 48404
Change your Pattern
to add a negative lookahead for the digit:
Pattern p = Pattern.compile("([A-Z]{1})(\\d+)([A-Za-z]+)(?!\\d)");
String str = "X89SuperJavaJavaX89SuperJava";
Output
X
89
SuperJavaJava
X
89
SuperJava
Upvotes: 3