Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex match each group separately

Tags:

java

regex

I have a string like this:

String text = "new SingleSizeProduct(422056, 1265858, 5430, '3XL', 75, 0, '14.90',     '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265859, 5341, 'L', 55, 0, '14.90',     '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265860, 5459, 'M', 45, 1, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265861, 5446, 'S', 35, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265862, 5458, 'XL', 60, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265863, 5511, 'XXL', 65, 0, '14.90', '16.50', '29.90', 'TL')";

and regex:

String regex = "new SingleSizeProduct((.*))";

I want to match all 6 groups separately, but when I match the pattern, I get result like this:

(
[0] => new SingleSizeProduct(422056, 1265858, 5430, '3XL', 75, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265859, 5341, 'L', 55, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265860, 5459, 'M', 45, 1, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265861, 5446, 'S', 35, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265862, 5458, 'XL', 60, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265863, 5511, 'XXL', 65, 0, '14.90', '16.50', '29.90', 'TL'),
[1] => (422056, 1265858, 5430, '3XL', 75, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265859, 5341, 'L', 55, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265860, 5459, 'M', 45, 1, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265861, 5446, 'S', 35, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265862, 5458, 'XL', 60, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265863, 5511, 'XXL', 65, 0, '14.90', '16.50', '29.90', 'TL'),
[2] => (422056, 1265858, 5430, '3XL', 75, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265859, 5341, 'L', 55, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265860, 5459, 'M', 45, 1, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265861, 5446, 'S', 35, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265862, 5458, 'XL', 60, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265863, 5511, 'XXL', 65, 0, '14.90', '16.50', '29.90', 'TL'),
)

How can I match each group separately?

like image 529
kakajan Avatar asked May 29 '14 13:05

kakajan


3 Answers

.* is greedy so it will try to find maximal possible match meaning that (.*) will match

abc(foo)def(bar)ghi
    ^^^^^^^^^^^

If you want to make it find minimal possible match

abc(foo)def(bar)ghi
    ^^^     ^^^

make * reluctant by adding ? after it

String regex = "new SingleSizeProduct((.*?))";

Also you need to escape ( and ) because as you know they represent start-end of capturing groups.

String regex = "new SingleSizeProduct\\((.*?)\\)";

BTW. Another solution would be using instead of .*? [^)]* which means, everything except )

String regex = "new SingleSizeProduct(([^)]*))";
like image 194
Pshemo Avatar answered Nov 07 '22 04:11

Pshemo


Escape the literal backslashes, and use a reluctant quantifier:

String regex = "new SingleSizeProduct\\((.*?)\\)";

(demo)

like image 30
arshajii Avatar answered Nov 07 '22 05:11

arshajii


You could use split here, assuming your input stays the same.

String text    = "new SingleSizeProduct(422056, 1265858, 5430, '3XL', 75, 0, '14.90',     '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265859, 5341, 'L', 55, 0, '14.90',     '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265860, 5459, 'M', 45, 1, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265861, 5446, 'S', 35, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265862, 5458, 'XL', 60, 0, '14.90', '16.50', '29.90', 'TL'),new SingleSizeProduct(422056, 1265863, 5511, 'XXL', 65, 0, '14.90', '16.50', '29.90', 'TL')";
String[] parts = text.split("(?<=\\)),");
System.out.println(Arrays.toString(parts));

Result:

[
 new SingleSizeProduct(422056, 1265858, 5430, '3XL', 75, 0, '14.90',     '16.50', '29.90', 'TL'), 
 new SingleSizeProduct(422056, 1265859, 5341, 'L', 55, 0, '14.90',     '16.50', '29.90', 'TL'), 
 new SingleSizeProduct(422056, 1265860, 5459, 'M', 45, 1, '14.90', '16.50', '29.90', 'TL'), 
 new SingleSizeProduct(422056, 1265861, 5446, 'S', 35, 0, '14.90', '16.50', '29.90', 'TL'), 
 new SingleSizeProduct(422056, 1265862, 5458, 'XL', 60, 0, '14.90', '16.50', '29.90', 'TL'), 
 new SingleSizeProduct(422056, 1265863, 5511, 'XXL', 65, 0, '14.90', '16.50', '29.90', 'TL')
]
like image 28
hwnd Avatar answered Nov 07 '22 04:11

hwnd