Regex nested / recursive pattern
I am trying to decode an encoded string k[encoded_string] to k * encoded_string. So, I am looking at using regex to accomplish this.
How to construct a regex pattern which has nested / recursive matches.
For example,
regex re("(\\d+)(\\[[a-zA-Z]+\\])");
str = "3[a]2[bc]";
This should match 3[a] and 2[bc] and it does:
Found: 2 matches
Match: 3[a]
Submatch 0: 3[a]
Submatch 1: 3
Submatch 2: [a]
Match: 2[bc]
Submatch 0: 2[bc]
Submatch 1: 2
Submatch 2: [bc]
However, for a string `”3[a2[c]]”`, I expect it to match:
2[c]3[adecoded(1)]
However, the regex I tried only manages to capture 1 match:
Found: 1 matches
Match: 2[c]
Submatch 0: 2[c]
Submatch 1: 2
Submatch 2: [c]
It misses the `3a[]` outer match which contains the 2[c] shown above. This is tricky.
Read more here: Source link
