Separate two matches

Rejax challenge: I want to receive groups twice, but don't know how to solve them.

Here is the 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:

X
89
SuperJavaJava
X
89
SuperJava

      

How can I separate two matches?

+3


source to share


3 answers


Since the last group ([A-Za-z]+)

will greedily match the next X, you couldn't get two rows. Use ((?:[A-Z][a-z]+)+)

to write words that are in this format FooBar

. Because the names don't end with a capital letter.

([A-Z])(\\d+)((?:[A-Z][a-z]+)+)

      

DEMO



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

      

+1


source


Modify yours Pattern

to add a negative result 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

      

+3


source


([A-Z]{1})(\d+)((?:(?!\1)[a-zA-Z])+)

      

Try it. Check out the demo.

http://regex101.com/r/sU3fA2/59

0


source







All Articles