I'm trying to match, using regex, all commas(followed by a space): ,
that are outside any parentheses or square brackets, i.e. the comma should not be contained in the parentheses or square brackets.
The target string is A, An(hi, world[hello, (hi , world) world]); This, These
. In this case, it should match the first comma and the last comma (the ones between A
and An
, this
and these
).
So I could split A, An(hi, world[hello, (hi , world) world]); This, These
into A
, An(hi, world[hello, (hi , world) world]); This
and These
, not leaving parens/brackets unbalanced as a result.
To that end, it seems hard to use regex alone. Is there any other approach to this problem?
The regex expression I'm using:
, (?![^()\[\]]*[\)\]])
But this expression will match other extra two commas ,
(the second and the third) which shouldn't have been matched.
Though if it is matching against the following strings, it'll match the right comma (the first one respectively): A, An(hi, world)
and A, An[hi, world]
But if the parenthesis and brackets contain each other, it'll be problems.
More details in this link: https://regex101.com/r/g8DOh6/1