String replacement involving special characters.

Discussion in 'Perl Misc' started by Blue, Jan 16, 2009.

  1. Blue

    Blue Guest

    $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
     
    Blue, Jan 16, 2009
    #1
    1. Advertising

  2. Blue

    J. Gleixner Guest

    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
     
    J. Gleixner, Jan 16, 2009
    #2
    1. Advertising

  3. Blue

    J. Gleixner Guest

    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/'/\\'/;
     
    J. Gleixner, Jan 16, 2009
    #3
  4. 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
     
    John W. Krahn, Jan 16, 2009
    #4
  5. Blue

    Tim Greer Guest

    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!
     
    Tim Greer, Jan 16, 2009
    #5
  6. 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


    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
     
    Jürgen Exner, Jan 16, 2009
    #6
  7. Blue

    Lars Eighner Guest

    In our last episode,
    <>, 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/>
    Bush's third term begins Jan. 20th with an invocation by Rick Warren.
    Obama: No hope; No change; More of the Same.
     
    Lars Eighner, Jan 16, 2009
    #7
  8. Blue

    Blue Guest

    On Jan 17, 3:59 am, Lars Eighner <> wrote:
    > In our last episode,
    > <>, 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.
     
    Blue, Jan 16, 2009
    #8
  9. Blue

    Uri Guttman Guest

    >>>>> "B" == Blue <> 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 ------ -------- 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 ---------
     
    Uri Guttman, Jan 16, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    3
    Views:
    33,316
    Stefan Mueller
    Jul 23, 2006
  2. Micah
    Replies:
    2
    Views:
    357
    Micah
    Jun 2, 2006
  3. Replies:
    2
    Views:
    1,147
    Ingo Menger
    May 31, 2007
  4. rvino
    Replies:
    0
    Views:
    4,722
    rvino
    Aug 14, 2007
  5. laredotornado

    special type of string replacement

    laredotornado, Feb 15, 2008, in forum: Ruby
    Replies:
    3
    Views:
    109
    Sebastian Hungerecker
    Feb 15, 2008
Loading...

Share This Page