Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: When a regexp with /g needs to be run .. twice

by haukex (Archbishop)
on Mar 02, 2018 at 21:26 UTC ( [id://1210261]=note: print w/replies, xml ) Need Help??


in reply to When a regexp with /g needs to be run .. twice

$foo = ':{"' $foo =~ s/([:,{])(.)/$1 $2/g;

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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1210261]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-19 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found