FAQ
Hi ,

I have a question regarding matching strings that have embeded perl special characters.

$abc = 'MC10.G(12)3c';

$efg = 'MC10.G(12)3c';

Now I want to check whether they are same or not by doing

if ($abc =~ /$efg/) { do something;}

For this I need to insert a '\' before every special char.

Can some one tell me an easy way to do ?

Regards
Rajeev

Search Discussions

  • Damon Davison at Sep 24, 2003 at 9:12 am
    On Wednesday 24 September 2003 10:54, Pandey Rajeev-A19514 wrote
    : I have a question regarding matching strings that have
    : embeded perl special characters.
    ...
    : Can some one tell me an easy way to do ?

    Have a look at \Q and \E. They're both listed in perldoc perlre
    under the main "Regular Expressions" heading.

    m/\Q.*\E./

    matches the literal string .*, followed by any character.

    Cheers,

    Damon

    --

    Damon Allen Davison
    davison@uni-koeln.de

    "A UNIX life is hard."
  • Sudarshan Raghavan at Sep 24, 2003 at 9:14 am

    Pandey Rajeev-A19514 wrote:
    Hi ,

    I have a question regarding matching strings that have embeded perl special characters.

    $abc = 'MC10.G(12)3c';

    $efg = 'MC10.G(12)3c';

    Now I want to check whether they are same or not by doing

    if ($abc =~ /$efg/) { do something;}
    Why not use the 'eq' operator
    if ($abc eq $efg) {#do something;}
    For this I need to insert a '\' before every special char.
    For academic interest, you can escape the special characters during
    matching using \Q and \E
    if ($abc =~ /\Q$efg\E/) { #do something;}
    Can some one tell me an easy way to do ?

    Regards
    Rajeev

  • Thomas Bätzler at Sep 24, 2003 at 9:35 am
    Hi,

    Pandey Rajeev-A19514 asked:
    I have a question regarding matching strings that have
    embeded perl special characters.

    $abc = 'MC10.G(12)3c';
    $efg = 'MC10.G(12)3c';

    Now I want to check whether they are same or not by doing
    if ($abc =~ /$efg/) { do something;}
    Why not use $abc eq $efg if you only want to check for
    equality?
    For this I need to insert a '\' before every special char.
    Can some one tell me an easy way to do ?
    You can also use the qr operator to quote metacharacters
    in a string or scalar value:

    my $re = qr( $efg );

    if( $abc =~ /$re/ ) {...}

    See the section "Regexp Quote-Like Operators" in the perlre
    manpage for details on qr().

    HTH,
    Thomas

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 24, '03 at 8:55a
activeSep 24, '03 at 9:35a
posts4
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase