0

I have written below regex

  "(?<customer>(\\w)+)" // to match c1 
        + "\\(((,)*" // To match (
        + "(?<product>(\\w)+),(?<likeness>(\\d)+)+)+" // to match p2,4
        + "\\)"; // To  match )

For below input:

C1(P2,51,P4,13)

Now Problem is when I execute

matcher.group("product");

It gives me p4, is there any way, I can get first p2 and then p4.

Vikas Singh
  • 2,838
  • 5
  • 17
  • 32
  • It is because the capture group will contain the last match which is `p4` – nu11p01n73R Nov 25 '14 at 17:35
  • Take a look at this [answer](http://stackoverflow.com/a/6020436/1025201) – mprabhat Nov 25 '14 at 17:35
  • Each capture group in your regex will return one substring. You cannot use capture groups to return multiple substrings from the source, even if the group is inside a pattern with `*` or `+`. regexes just aren't set up to do that. Sorry. – ajb Nov 25 '14 at 17:46
  • You can capture the whole thing inside the bracket and do a second pass on the data (by splitting or matching - up to you). There is the second method of rewriting your regex, but I do not recommend it: http://stackoverflow.com/questions/15268504/collapse-and-capture-a-repeating-pattern-in-a-single-regex-expression/15418942#15418942 – nhahtdh Nov 26 '14 at 02:18

0 Answers0