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

I have the below code. it's not printing "yes". I think the special character appears "(" in $text. How can i solve that. i have these two texts in two seperate files. for example i have given this code.
$text='sd(sd)sd'; $a='sd(sd)sd'; if ($text=~/$a/) { print "yes"; } else { print "no"; }

Replies are listed 'Best First'.
Re: pattern match special char
by moritz (Cardinal) on Sep 11, 2009 at 11:17 UTC

    With quotemeta or /\Q$a\E/. See perlre for details.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: pattern match special char
by chorankates (Novice) on Sep 11, 2009 at 15:02 UTC
    i think you just need to escape the "(" in your regular expression:
    $a = 'sd\(sd\)sd';
    it's now printing "yes" for me.
Re: pattern match special char
by bichonfrise74 (Vicar) on Sep 11, 2009 at 17:48 UTC
    As mortiz pointed out, you can use quotemeta.
    #!/usr/bin/perl use strict; my $text = 'sd(sd)sd'; my $a = quotemeta( 'sd(sd)sd' ); if ($text =~ /$a/ ) { print "yes"; } else { print "no"; }