Perl-Sensitive Sunglasses | |
PerlMonks |
Re: When a regexp with /g needs to be run .. twiceby haukex (Archbishop) |
on Mar 02, 2018 at 21:26 UTC ( [id://1210261]=note: print w/replies, xml ) | Need Help?? |
The first match of the regex matches :{, and then the regex engine continues looking after that match, but the only thing there is ", which does not match your regex, which expects two characters. This works (although not yet tested on a lot of different inputs): s/(?<=[:,{])(?=.)/ /g (using Zero-Width Lookaround Assertions) occurrences of left square bracket, colon and left brace Left square brackets, or commas? You've got [:,{] Of course, if you're cleaning up JSON, it might be better to just round trip it through a module that can pretty-print it?
In Section
Seekers of Perl Wisdom
|
|