Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Regular expression match trouble with "+"

by kwyjibo (Initiate)
on Aug 16, 2006 at 14:39 UTC ( [id://567700]=perlquestion: print w/replies, xml ) Need Help??

kwyjibo has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Source code speaks for itself. It should be printing "Ok", but strings don't match... I suspect that is taking the "+" in the strings as an operator, or something like that. Any workaround? Thanks in advance.
$a = "I have to get the + version"; $b = "I have to get the + version"; if($a =~ m/$b/i) { print "Ok\n"; } else { print "Not the same...\n"; }

Replies are listed 'Best First'.
Re: Regular expression match trouble with "+"
by davorg (Chancellor) on Aug 16, 2006 at 14:42 UTC

    You need \Q...\E which quotes metacharacters in regexes.

    if ($a =~ m/\Q$b\E/)

    See also quotemeta

    By the way, $a and $b have a special meaning to Perl (because of their use in sorting) so it's a really bad idea to use them for anything else.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Regular expression match trouble with "+"
by ikegami (Patriarch) on Aug 16, 2006 at 14:43 UTC

    Your suspicions are correct. The + is a special character in regexps. You need to escape it. Here are two methods of doing it programatically:

    if ($a =~ m/\Q$b\E/i)
    $b = quotemeta($b); if ($a =~ m/$b/i)

    References: perlre and quotemeta

    Update: It's much faster to use string functions when dealing which constant strings. Case-insensitivity is obtained by converting both strings to a common case using lc or uc.

    if (lc($a) eq lc($b))
Re: Regular expression match trouble with "+"
by artist (Parson) on Aug 16, 2006 at 15:43 UTC
    Look for perldoc -q regex and perldoc -f quotemeta. I think that it's the proper way to do it. I don't know why converting to lower case via lc() also works.
    --Artist
      It's the use of eq instead of m// that makes it work, not the conversion to lowercase. Converting to lowercase is to string compares what /i is to regexps.
Re: Regular expression match trouble with "+"
by xorl (Deacon) on Aug 16, 2006 at 14:46 UTC
    Only thing I can think of...
    #!/usr/bin/perl use strict; use warnings; $a = "I have to get the + version"; $b = "I have to get the + version"; # if($a =~ m/$b/i) if (lc($a) eq lc($b)) { print "Ok\n"; } else { print "Not the same...\n"; }
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://567700]
Approved by xorl
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-24 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found