Reputation: 342
I need to capture in perl everything between { and },{ in a string. Occurrences can exists from 0 to a finite number.
I've tried to use:
my @tmp;
my $extract = "";
$extract = $1 if $json =~ /\{([^}]+)\}/g;
push @tmp, $extract;
Where $json is the string where I need to extract the content.
Here is an example of two items of $json:
{"id":"AAAAAAAAAA","text":"AAAAAAAAAA","icon":"jstree-folder","li_attr":{"id":"AAAAAAAAAA"},"a_attr":{"href":"#","id":"AAAAAAAAAA_anchor"},"state":{"loaded":true,"opened":true,"selected":false,"disabled":false},"data":{"Taginfo":"default","Type":"","Measure":"","Scale":"default","Filter":"","Concept":"default","Uso":"default","Uso2":"default","Parciales":1},"parent":"#"},{"id":"BBBBBBBBBBBBB","text":"BBBBBBBBBBBBB","icon":"jstree-folder","li_attr":{"id":"BBBBBBBBBBBBB"},"a_attr":{"href":"AAAAAAAAAA","id":"BBBBBBBBBBBBB_anchor"},"state":{"loaded":true,"opened":false,"selected":true,"disabled":false},"data":{"Taginfo":"BBBBBBBBBBBBB","Type":"default","Measure":"default","Scale":"1000","Filter":"1000","Concept":"default","Uso":"","Uso2":"","Parciales":1},"parent":"AAAAAAAAAA"}
Using the regex above, I'm not getting the correct string. i.e: In the first occurrence has to be:
{"id":"AAAAAAAAAA","text":"AAAAAAAAAA","icon":"jstree-folder","li_attr":{"id":"AAAAAAAAAA"},"a_attr":{"href":"#","id":"AAAAAAAAAA_anchor"},"state":{"loaded":true,"opened":true,"selected":false,"disabled":false},"data":{"Taginfo":"default","Type":"","Measure":"","Scale":"default","Filter":"","Concept":"default","Uso":"default","Uso2":"default","Parciales":1},"parent":"#"}
So, I need to capture until the next:
},{
My question is how to ignore end the capture group with },{ instead of just with }?
Thanks.
Upvotes: 0
Views: 175
Reputation: 784998
You actually need to use a recursive matching pattern to match closing { .. }
like this:
\{(?:[^{}]+|(?0))*}
Here (?0)
recurses the entire pattern.
Though if you are parsing a valid JSON string then it is better to use JSON parser.
Upvotes: 2