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

I am having problem with the below code. Both are equal but if "+" symbol appears then it's not find correctly. how can i solve this?
$var='/Sample + Design'; $var1='/Sample + Design'; if (($var1=~/$var/)) { print "Yes"; }

Replies are listed 'Best First'.
Re: match with symbol
by Corion (Patriarch) on Jul 22, 2009 at 07:53 UTC
Re: match with symbol
by JavaFan (Canon) on Jul 22, 2009 at 09:11 UTC
    Or Corion points out, one way is to use \Q and \E. Another is to realize that if you want an exact match, there's no need to use the regexp engine.
    if (index($var, $var1) >= 0) { print "Yes"; }
Re: match with symbol
by chomzee (Beadle) on Jul 22, 2009 at 09:13 UTC
    Did you mean:
    print "yes\n" if ($var1 eq $var);
    ?
    Your regexp doesn't match because '+' has a special meaning there. You should use:
    $var='/Sample \+ Design';
      Hey as said earlier in patternmatch the special character has different meaning, so we need to put backslash infront of those chars. $var = '/Sample + Design'; $var1 = '\/Sample \+ Design'; if($var =~/$var1/) { print "yes \n"; } else{ print " No \n"; } Thanks claiming