Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

searching for parentheses in perl

Tags:

regex

perl

Writing a program where I read in a list of words/symbols from one file and search for each one in another body of text.

So it's something like:

while(<FILE>){
    $findword = $_;

    for (@text){
        if ($_=~ /$find/){
            push(@found, $_);
        }
    }
}

However, I run into trouble once parentheses show up. It gives me this error:

Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE

I realize it's because Perl thinks the ( is part of the regex, but how do I deal with this and make the ( searchable?

like image 444
user986087 Avatar asked Dec 27 '22 13:12

user986087


1 Answers

You could use \Q and \E:

if ($_ =~ /\Q$find\E/){

Or just use index if you're just looking for a literal match:

if(index($_, $find) >= 0) {
like image 195
mu is too short Avatar answered Jan 08 '23 22:01

mu is too short