Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

String replacement involving special characters.

2 views
Skip to first unread message

Blue

unread,
Jan 16, 2009, 1:57:37 PM1/16/09
to
$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. Gleixner

unread,
Jan 16, 2009, 2:11:05 PM1/16/09
to

Escape it.

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

J. Gleixner

unread,
Jan 16, 2009, 2:14:13 PM1/16/09
to

sorry.. should have removed 'bb' there.

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

John W. Krahn

unread,
Jan 16, 2009, 2:21:45 PM1/16/09
to
Blue wrote:
> $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
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov

Tim Greer

unread,
Jan 16, 2009, 2:27:08 PM1/16/09
to
Blue wrote:

$string =~ s/'/\\'/g;
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Jürgen Exner

unread,
Jan 16, 2009, 2:38:43 PM1/16/09
to
Blue <mrlawr...@gmail.com> wrote:
>$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

Lars Eighner

unread,
Jan 16, 2009, 2:59:52 PM1/16/09
to
In our last episode,
<67d58bc8-d9fe-45fd...@a29g2000pra.googlegroups.com>, the
lovely and talented Blue broadcast on comp.lang.perl.misc:

To enter a literal backslash, use two backslashes.

--
Lars Eighner <http://larseighner.com/> use...@larseighner.com
Bush's third term begins Jan. 20th with an invocation by Rick Warren.
Obama: No hope; No change; More of the Same.

Blue

unread,
Jan 16, 2009, 5:47:11 PM1/16/09
to
On Jan 17, 3:59 am, Lars Eighner <use...@larseighner.com> wrote:
> In our last episode,
> <67d58bc8-d9fe-45fd-a45b-949fa8f3c...@a29g2000pra.googlegroups.com>, the

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

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

Uri Guttman

unread,
Jan 16, 2009, 6:39:15 PM1/16/09
to
>>>>> "B" == Blue <mrlawr...@gmail.com> writes:

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

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

0 new messages