How to match but exclude delimiters in recursive pattern

Viewed 72

Assume a string {a:b,c:d},{a:b,c:d,e:[{d:e},{f:g}]}. I'd like to parse this into tokens a:b,c:d a:b,c:d,e:[{d:e},{f:g}] which later can be parsed further.

The regex ({(?:[^{}]++|(?1))*}) produces {a:b,c:d} {a:b,c:d,e:[{d:e},{f:g}]}, but I'm stuck in figuring out how to match on the enclosing {} without including them in the result. (The string may contain even deeper recursion elements not shown in the example)

1 Answers

You need to capture the part of the regex you need to extract from the match:

{((?:[^{}]++|(?R))*)}
 ^_________________^

These parentheses create Group 1 that will hold the value you can access using your programming language.

Once you get the matches, you may run a second pass to extract a:b or a:[...] substrings using

[^,[]+(?:\[[^][]+])?

See the regex demo. Details:

  • [^,[]+ - zero or more chars other than [ and comma
  • (?:\[[^][]+])? - an optional sequence of [, then one or more chars other than [ and ] and then a ].
Related