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

Hola Monks, I am using the =~ to search for an instance of a certian string in an array, but every time one of the strings in the array contains special chars. I get an error:
Unmatched ( in regex; marked by <-- HERE in m/.*"blah blah ( <-- HERE +blah blah" .*/ at blah.pl line 53.
Here's the code:
if ($lhs[0] =~ /.*$rhs.*/) { $matches++; } else { print "Error: $lhs[0] = $rhs[1] \n"; diff++; }

2005-01-07 Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Variable in regex...
by Roy Johnson (Monsignor) on Jan 07, 2005 at 21:05 UTC
    Death to dot-star!

    The right tool for this job is index, not a regex. Also, use code tags in your post, so your brackets don't get specially interpreted.


    Caution: Contents may have been coded under pressure.
Re: Variable in regex...
by amw1 (Friar) on Jan 07, 2005 at 20:57 UTC
    try
    if($lhs[0] =~ /.*\Q$rhs[1]\E.*/) { $matches++; } else { print "Error: $lhs[0] = $rhs[1]\n"; $diff++; }
Re: Variable in regex...
by trammell (Priest) on Jan 07, 2005 at 21:47 UTC
Re: Variable in regex...
by ambrus (Abbot) on Jan 07, 2005 at 21:42 UTC
Re: Variable in regex...
by ww (Archbishop) on Jan 07, 2005 at 21:13 UTC
    Also, when you get error messages (of the sort you posted) on a regex, give us enough literal output to UNambiguously id what the reference is, just before the second:
    <--HERE
    The second "<--HERE" in your error line tells EXACTLY where the error exists. In some regexen, especially those with multiple similar elements, knowing that the error occured at a close_paren may not be enough.

    In this case (even before the janitorial services) spotting the error was not hard, but note also the suggestion for an alternate approach.

    Updated to clarify (originally sloppy) syntax in first paragraph.

Re: Variable in regex...
by perlsen (Chaplain) on Jan 08, 2005 at 09:48 UTC

    you have to use the quotemeta for your search string to include the special charecter in your search

    quotemeta($rhs); or \q$rhs[1]\E

    Regards,

    Senthil Kumar.k