views:

45

answers:

2

Hello,

I have following regex (abc|def)( ?(\\d+|(?:(?!\\1)[a-z])+)?)* with matches perfectly the subject abc123 456.
Now I want to get all parts abc, 123 and 456.

I use following code:

    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(subject);

    while(m.find())
    {
        System.out.println(m.group());
    }

But so I get only abc123 456.

Any ideas are welcome.

+1  A: 

You have to get each group individually instead of m.group(). The javadoc states that m.group() is equivalent to m.group(0), and individual groups can be accessed with the m.group(int) version. So the following assertions should reflect the grouping as you expect.

Assert.assertEquals("abc", m.group(1));
Assert.assertEquals("123", m.group(2));
Assert.assertEquals("456", m.group(3));
Cem Catikkas
Thanks for your advice. m.group(1) gives back abc, m.group(2) nothing and m.group(3) 456. So I don't get what I want. I want to get all three parts alone.
H3llGhost
A: 

I used the solution explained in my comment above:

Yes it works, but I notice that I can simplify the regex. abc|def are commands and then there are the parameters seperated by space. And I search for a way to get them. But I think it is easier to cut off the command and split by space, isn't it?

H3llGhost