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?
.*
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(([^)]*))";
Escape the literal backslashes, and use a reluctant quantifier:
String regex = "new SingleSizeProduct\\((.*?)\\)";
(demo)
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')
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With