Jim Jim - 1 year ago 73
Perl Question

How can I substitute double backslash with 1?

Having a string that has the backslash escaped i.e.

how can I make it single backslash?

$string =~ s/\\/\/g;
does not work.

Jan Jan
Answer Source

You need four backslashes as the backslash itself needs to be escaped as well:

$string =~ s/\\\\/\\/g

See a demo on regex101.com.

A complete example in Perl:

my $string = 'this is a double backslash: \\\\';
print $string . "\n";
$string =~ s/\\\\/\\/g;
print $string;

See the demo on ideone.com.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download