5

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?

3 Answers 3

3

.* 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(([^)]*))";
Sign up to request clarification or add additional context in comments.

1 Comment

taxn, it solved my issue,String regex = "new SingleSizeProduct\((.*?)\)";
2

Escape the literal backslashes, and use a reluctant quantifier:

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

(demo)

1 Comment

tanx for quick response too
1

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')
]

1 Comment

+1 assuming that input contains only new SingleSizeProduct(...) values. In case other values are possible like , new Foo(...) Pattern/Matcher would be better approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.