replacing backslash

A

Adam

Hi, I wanna replace a single backslash with two backslashes.
So i've tried:

$texT =~ s/\/\\/g;
$texT =~ s/chr(92)/chr(92).chr(92)/g;

and a couple of others, but none of them seems to work. Which line
would do the trick ?
 
M

MikeGee

You probably want:

$texT =~ s/\\/\\\\/g;

because \ is the escape character in both regex patterns and double
quoted strings. So a literal \ is written \\ on both sides of s///.

-Mike
 
A

Adam

Thanks for replying.. I tested that, but it doesnt give the desired
result, namely a output with 2 backslashes. Í've used this for
testing:

$texT="This \ is my test";
$texT =~ s/\\/\\\\/g;
print $texT;
 
J

John W. Krahn

Adam said:
Thanks for replying.. I tested that, but it doesnt give the desired
result, namely a output with 2 backslashes. Í've used this for
testing:

$texT="This \ is my test";
$texT =~ s/\\/\\\\/g;
print $texT;

Run your test like this:

$texT="This \ is my test";
print $texT;
$texT =~ s/\\/\\\\/g;
print $texT;

And you will see that there is no backslash in your test string!


John
 
I

it_says_BALLS_on_your forehead

Adam said:
Thanks for replying.. I tested that, but it doesnt give the desired
result, namely a output with 2 backslashes. Í've used this for
testing:

$texT="This \ is my test";
$texT =~ s/\\/\\\\/g;
print $texT;

that's because your initial string is in double quotes, and not single
quotes. Perl is interpolating the '\ ' to mean an esaped space, which
is essentially a space. try putting your initial string in single
quotes.
 
I

it_says_BALLS_on_your forehead

it_says_BALLS_on_your forehead said:
that's because your initial string is in double quotes, and not single
quotes. Perl is interpolating the '\ ' to mean an esaped space, which
is essentially a space. try putting your initial string in single
quotes.

perhaps i should have written that Perl is interpolating the "\ " to
mean an escaped space.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,497
Latest member
PilarLumpk

Latest Threads

Top