Win8 Strawberry 5.30.3.1 (64) Wed 09/07/2022 8:14:56 C:\@Work\Perl\monks >perl use 5.010; # needs \K regex extension use strict; use warnings; # use Data::Dump qw(dd); # for debug my $j = <<'EOJ'; "device\"": [{"host_in}fo" : { "eng{ine_id,":null , "name:": null ,"host_group_info\\" :{"na\"me":null,"\"remarks":null , "id" : 0 } "{[:," : "weird", "crazy" : "[{,:" }} ] EOJ print ">>\n$j<< \n"; use constant DENT => ' '; my $n_dent = 0; my %pretty = ( '{' => sub { my $d = DENT x ++$n_dent; return "$_[0]\n$d"; }, '}' => sub { my $d = DENT x --$n_dent; return "\n$d$_[0]\n$d"; }, ',' => sub { my $d = DENT x $n_dent; return "$_[0]\n$d"; }, ':' => sub { my $d = DENT x $n_dent; return "$_[0] "; }, ); my $rx_d_quote = qr{ " [^\\"]* (?: \\. [^\\"]*)* " }xms; $j =~ s{ $rx_d_quote? \K \s* ([{},:]) \s* } { $pretty{$1}->($1) }xmsge; print ">>\n$j<< \n"; ^Z >> "device\"": [{"host_in}fo" : { "eng{ine_id,":null , "name:": null ,"host_group_info\\" :{"na\"me":null,"\"remarks":null , "id" : 0 } "{[:," : "weird", "crazy" : "[{,:" }} ] << >> "device\"": [{ "host_in}fo": { "eng{ine_id,": null, "name:": null, "host_group_info\\": { "na\"me": null, "\"remarks": null, "id": 0 } "{[:,": "weird", "crazy": "[{,:" } } ] <<