Struggle with "simple" replacement

Discussion in 'Perl Misc' started by Arne, Jun 15, 2004.

  1. Arne

    Arne Guest

    Trying to make a simple replacement of strings I spent some hours
    resulting in this:

    #!/usr/bin/perl -w
    #use strict;

    $_ = "Just {Test4*4}";
    #my $old = "{Test4*4}"; #(i) normal double quoted match
    my $old = "\{Test4\*4\}"; #(ii) escaping meta chars
    #my $old = '{Test4*4}'; #(iii) using single quotes
    my $new = "okay";
    s/$old/$new/;
    print "$_\n";

    It should just demonstrate how to substitute a substring containing
    meta chars (e.g. { or *). I had expected that avoiding interpolation
    by escaping would help. But even single quote strings aren't better.
    Tell me why I am such a fool. would appreciate.

    Arne
     
    Arne, Jun 15, 2004
    #1
    1. Advertising

  2. Arne

    Sam Holden Guest

    On 14 Jun 2004 21:59:11 -0700, Arne <> wrote:
    > Trying to make a simple replacement of strings I spent some hours
    > resulting in this:
    >
    > #!/usr/bin/perl -w
    > #use strict;
    >
    > $_ = "Just {Test4*4}";
    > #my $old = "{Test4*4}"; #(i) normal double quoted match
    > my $old = "\{Test4\*4\}"; #(ii) escaping meta chars


    That doesn't escape anything. { and * are not "special" in
    a double quoted string so the backslashes are essentially ignored
    (or \{ is converted to { and so on).

    > #my $old = '{Test4*4}'; #(iii) using single quotes
    > my $new = "okay";
    > s/$old/$new/;
    > print "$_\n";
    >
    > It should just demonstrate how to substitute a substring containing
    > meta chars (e.g. { or *). I had expected that avoiding interpolation
    > by escaping would help. But even single quote strings aren't better.
    > Tell me why I am such a fool. would appreciate.


    perldoc -f quotemeta

    or to do things the hard way:

    "\\{Test4\\*4\\}"

    or

    '\{Test4\*4\}'

    --
    Sam Holden
     
    Sam Holden, Jun 15, 2004
    #2
    1. Advertising

  3. Arne

    Arne Guest

    Truth is simple:

    > perldoc -f quotemeta


    is the golden hint. Thank u so much Sam. (was my first posting so that
    I cannot back off...)

    Arne
     
    Arne, Jun 15, 2004
    #3
    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. Youhanna Mounir
    Replies:
    3
    Views:
    560
  2. =?Utf-8?B?TWljaGFlbCBHZWlzdA==?=

    Struggle with The Move to .NET

    =?Utf-8?B?TWljaGFlbCBHZWlzdA==?=, Nov 23, 2004, in forum: ASP .Net
    Replies:
    21
    Views:
    736
    Scott M.
    Nov 24, 2004
  3. jay

    Undo Operation Struggle

    jay, Feb 24, 2006, in forum: Java
    Replies:
    6
    Views:
    567
  4. Ryan Liu
    Replies:
    1
    Views:
    611
    Alexey Smirnov
    Feb 5, 2009
  5. Phil H
    Replies:
    1
    Views:
    96
    Nathan Sokalski
    Dec 21, 2007
Loading...

Share This Page