String replacement involving special characters.

B

Blue

$string = "aaa ' ccc ";
$string =~ s/'/bbb/eg;

The above replaces the single-quotation mark with bbb. The result will
be:
aaa bbb ccc

How do I modify it so that the single-quotation mark is replaced with
\' (a backslash and a single-quotation mark) resulting in:
aaa \' ccc
 
J

J. Gleixner

Blue said:
$string = "aaa ' ccc ";
$string =~ s/'/bbb/eg;

The above replaces the single-quotation mark with bbb. The result will
be:
aaa bbb ccc

How do I modify it so that the single-quotation mark is replaced with
\' (a backslash and a single-quotation mark) resulting in:
aaa \' ccc

Escape it.

$string = "aaa ' ccc ";
$string =~ s/'/\\'bb/; #why are you using /eg?
print $string;
aaa \'bb ccc
 
J

J. Gleixner

J. Gleixner said:
Escape it.

$string = "aaa ' ccc ";
$string =~ s/'/\\'bb/; #why are you using /eg?
print $string;
aaa \'bb ccc

sorry.. should have removed 'bb' there.

$string =~ s/'/\\'/;
 
J

John W. Krahn

Blue said:
$string = "aaa ' ccc ";
$string =~ s/'/bbb/eg;

Because of the /e option you are evaluating the string 'bbb' as though
it were perl code. Why are you doing that?

The above replaces the single-quotation mark with bbb. The result will
be:
aaa bbb ccc

How do I modify it so that the single-quotation mark is replaced with
\' (a backslash and a single-quotation mark) resulting in:
aaa \' ccc

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



John
 
T

Tim Greer

Blue said:
$string = "aaa ' ccc ";
$string =~ s/'/bbb/eg;

The above replaces the single-quotation mark with bbb. The result will
be:
aaa bbb ccc

How do I modify it so that the single-quotation mark is replaced with
\' (a backslash and a single-quotation mark) resulting in:
aaa \' ccc

$string =~ s/'/\\'/g;
 
J

Jürgen Exner

Blue said:
$string = "aaa ' ccc ";
$string =~ s/'/bbb/eg;

The above replaces the single-quotation mark with bbb. The result will
be:
aaa bbb ccc

Hmmm, for me the result was
Bareword "bbb" not allowed while "strict subs" in use at ...

Which is not surprising because why on earth are you evaluating 'bbb'?
And why are you applying the substitution globally although there is
only one quote sign in the text?
How do I modify it so that the single-quotation mark is replaced with
\' (a backslash and a single-quotation mark) resulting in:
aaa \' ccc

Just put the new text there. Just remember that the desired backslash is
the escape character. Therefore if you want a literal backslash in the
result, then you have to escape the escape character. Oh, and get rid of
those bogus options, too:

$string =~ s/'/\\'/;

jue
 
L

Lars Eighner

In our last episode,
the said:
$string = "aaa ' ccc ";
$string =~ s/'/bbb/eg;
The above replaces the single-quotation mark with bbb. The result will
be:
aaa bbb ccc
How do I modify it so that the single-quotation mark is replaced with
\' (a backslash and a single-quotation mark) resulting in:
aaa \' ccc

To enter a literal backslash, use two backslashes.
 
B

Blue

In our last episode,


To enter a literal backslash, use two backslashes.

$string =~ s/'/\\'/;

Thanks guys. I thought I did the above and got Internal Server Error.
I did it again and it is working. Thanks.
 
U

Uri Guttman

B> Thanks guys. I thought I did the above and got Internal Server Error.
B> I did it again and it is working. Thanks.

you should not debug your programs via a web server. that is making it
much harder for you. run the scripts locally on your dev box and then
upload to your server. and don't say you don't have perl on your box
because you can install it easily. and don't say you can't test web
programs on your box because again, you can set up a web server on your
box with little effort. and the term internal server error should be never
be used in a perl group as it is a web server issue. report the actual
perl bug by running it outside a web server

uri
 

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
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top