in reply to matching the + character

+ 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

Replies are listed 'Best First'.
Re: Re: matching the + character
by Anonymous Monk on Aug 06, 2001 at 22:10 UTC
    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
Re: Re: matching the + character
by jalebie (Acolyte) on Aug 06, 2001 at 21:48 UTC
    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