Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > String replacement involving special characters.

Reply
Thread Tools

String replacement involving special characters.

 
 
Blue
Guest
Posts: n/a
 
      01-16-2009
$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
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      01-16-2009
Blue wrote:
> $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
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      01-16-2009
J. Gleixner wrote:
> Blue wrote:
>> $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


sorry.. should have removed 'bb' there.

$string =~ s/'/\\'/;
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-16-2009
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
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      01-16-2009
Blue wrote:

> $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;
--
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!
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      01-16-2009
Blue <(E-Mail Removed)> 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
 
Reply With Quote
 
Lars Eighner
Guest
Posts: n/a
 
      01-16-2009
In our last episode,
<(E-Mail Removed)>, the
lovely and talented Blue broadcast on comp.lang.perl.misc:

> $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.

--
Lars Eighner <http://larseighner.com/> http://www.velocityreviews.com/forums/(E-Mail Removed)
Bush's third term begins Jan. 20th with an invocation by Rick Warren.
Obama: No hope; No change; More of the Same.
 
Reply With Quote
 
Blue
Guest
Posts: n/a
 
      01-16-2009
On Jan 17, 3:59*am, Lars Eighner <(E-Mail Removed)> wrote:
> In our last episode,
> <(E-Mail Removed)>, the
> lovely and talented Blue broadcast on comp.lang.perl.misc:
>
> > $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.
>
> --
> * * * * 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.


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

Thanks guys. I thought I did the above and got Internal Server Error.
I did it again and it is working. Thanks.
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      01-16-2009
>>>>> "B" == Blue <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- 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 ---------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
special type of string replacement laredotornado Ruby 3 02-15-2008 04:52 PM
string hashing design/implementation questions involving stringliterals Sebastian Karlsson C++ 4 02-13-2008 10:54 AM
Special editions and Deluxe special edition dvd question. Rclrk43 DVD Video 8 12-29-2004 07:32 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments