1

I would like to extract all the values separated by comma(,) after matching with pattern.

So first I am matching my string with regex and then using Matcher to extract values.

Regex= \(([^)]+)\) is matching correctly following string without any issue .

('A', '36254632546', 0, 'Test, test1', NULL) 

But unable to match when string convert(datetime, 'Dec 27 2016 10:36:54', 116) is available in original string. I tried matching last ) by putting $ at the end but seems not working.

String to match = ('A', convert(datetime, 'Dec 27 2016 10:36:54', 116), 0, 'Test, test1', NULL)

amicngh
  • 7,543
  • 3
  • 26
  • 48
  • 3
    See [How to match string within parentheses (nested) in Java?](http://stackoverflow.com/questions/17759004/how-to-match-string-within-parentheses-nested-in-java). Also, [this snippet](http://stackoverflow.com/a/37207892/3832970) might help you. You cannot use a regex if the nested level number is arbitrary because Java regex does not support recursion. – Wiktor Stribiżew Dec 27 '16 at 11:53
  • It seems like you are parsing some programming language. Are you sure that regular expressions will be able to *match* all possible input? – GhostCat Dec 27 '16 at 11:53
  • Please try on this https://regex101.com/ – amicngh Dec 27 '16 at 11:56

1 Answers1

3

This is because your regex stops at the first ')'.

What you say in regex is start with '(' then group everything except ')' followed by ')' so then it stops even if you put dollar sign at the end because the end is not there you have remaining string after the first ')'.

The '+' sign is for consecutive chars.

If you would like to match all the string you have to tell something like

\\(([^)]+\\)[^)]+)\\);

but this has to do with your program's logic.

Also if you want to much groups of commas you have to change your regex, so that the groups are groups of commas.

ddarellis
  • 3,762
  • 3
  • 23
  • 51