in reply to When a regexp with /g needs to be run .. twice
The following regex:
is matching two characters, i.e. :{, so that the regex engine will continue on the next character, i.e. ", which cannot be matched by the regex. So, in short, your regex is not applied a second time on your input string. It has gotten too far already in your input string.$foo =~ s/([:,{])(.)/$1 $2/g;
If you want the regex engine to repeatedly match each of the input characters, please remove the (.) part:
For example (demonstrated under the Perl debugger):$foo =~ s/([:,{])/$1 /g;
Update:: this is essentially the same solution as toolic's proposal, I did not notice until I reviewed the thread after having posted my response.DB<1> $foo = ':{"'; DB<2> $foo =~ s/([:,{])/$1 /g; DB<3> print $foo : { "
|
|---|