Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

matching the + character

by jalebie (Acolyte)
on Aug 06, 2001 at 21:40 UTC ( #102519=perlquestion: print w/replies, xml ) Need Help??

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

For some reason this code doesn't seem to match properly even though both the variables are the same.
use strict; my $var1 = "a+b"; my $var2 = "a+b"; if ($var1 =~ /$var2/) { print "hey it matches \n"; } print "hey here \n";
I know that the + is a special metacharacter, and have even tried making $var2 = "a\+b" but for some reason this doesnt work for me either. The only thing that works is when i have 2 forward slashes before the + character like this. $var2 = "a\\+b"; I cannot figure out a logical reason for this. Please if anyone can help me, This is actually a part of a bigger program that is failing when it sees the + character Waris

Replies are listed 'Best First'.
Re: matching the + character
by dragonchild (Archbishop) on Aug 06, 2001 at 21:45 UTC
    Because you're using double-quotes (""), you need to backslash the backslash. :) If you used single-quotes ('') like 'a\+b', you'll be just fine.

    The lesson here is to use the minimal quoting mechanism. Unless you're interpolating variables, use ''. If you're interpolating variables, remember that everything is interpolated.

    ------
    /me wants to be the brightest bulb in the chandelier!

Re: matching the + character
by thpfft (Chaplain) on Aug 06, 2001 at 22:35 UTC

    Single quoting and character classes are all very well if you're dealing with test data, but i think we can assume you're not really going to be testing whether two strings you just typed in match. In the general case, if you want to find out whether $var2 is contained in $var1, and $var2 might contain something that will be unhelpfully interpolated, use this:

    $match if $var1 =~ /\Q$var2\E/;

    \Q..\E is a special case of the quotemeta function, which automatically escapes non-alphanumerics (but not $ or @, so you still get variable interpolation).

    Not the first time this has come up, by the way. Reading perlre will help.

    update: followed own sanctimonious advice, read perlre, changed description of quotemeta :(

Re: matching the + character
by suaveant (Parson) on Aug 06, 2001 at 21:43 UTC
    + is a special character in regular expressions that means match one or more of what is before me... your regexp will match one or more a's followed by a b... i.e. aaab ab aaaaaab, to match a plus you must escape it, using a \ so \+ will match a plus... see perlre

    Update Doh, my eyes skipped the lower text... :)

    you need the \\ because "" interploates data, thus converting \+ to + before it gets to the regexp... if you use single quotes 'a\+b' then you will be ok.

                    - Ant
                    - Some of my best work - Fish Dinner

      use strict; 3 my $var1 = "a+b"; 4 my $var2 = "a+b"; 5 if ($var1 =~ /\Q$var2\E/) { 6 print "hey it matches \n"; 7 } 8 9 print "hey here \n"; 10
      See Line : 5
      I know that thats i even tried matching by setting
      use strict; my $var1 = "a+b"; my $var2 = "a\+b"; if ($var1 =~ /$var2/) { print "hey it matches \n"; } print "hey here \n";
      but it doesn't work. AGAIN the only thing that works is
      $var2 = "a\\+b";
      please explain Waris
Re: matching the + character
by kilinrax (Deacon) on Aug 06, 2001 at 21:47 UTC
    In a double quoted string, '\+' will be interpreted as a character escape. Since '\+' doesn't actually have any special character escape meaning, it is interpreted as simply '+'. If you want to pass 'a\+b' to the regex, you'll have to use a second slash to escape the first and prevent any attempt at character escape interpretation gobbling up the '\'.
Re: matching the + character
by lshatzer (Friar) on Aug 06, 2001 at 22:02 UTC
    Along with what everyone else has said above, the function quotemeta comes to mind. Check it out, it will help you when you have other characters you need to quote properly also...
Re: matching the + character
by dga (Hermit) on Aug 06, 2001 at 22:12 UTC

    Also to get rid of pesky characters like + in re's you could make a character class.

    Another way to do it.

    use strict; my $var1 = "a+b"; my $var2 = "a[+]b"; if ($var1 =~ /$var2/) { print "hey it matches \n"; } print "hey here \n";
Re: matching the + character
by foogod (Friar) on Aug 06, 2001 at 22:16 UTC

    just 'cuz I have time today ... here is what the real code should be...

    use strict; my $var1 = 'a+b'; my $var2 = 'a+b'; if ($var1 =~ /$var2/) { print "hey it matches \n"; } print "hey here \n";

    the posts above already said why it isnt working ... but now you have code that will actaully work (that is like yours).

    - f o o g o d

    --- ruining the bell curve for everyone else ---

Re: matching the + character
by mamboking (Monk) on Aug 07, 2001 at 01:50 UTC
    Do you really need to use a regexp? Perhaps you could just use index() or even "eq".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2023-02-01 18:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (12 votes). Check out past polls.

    Notices?