3

I have a variable $next which contains strings that might contain parenthesis e.g trna(tgc) I want to make this matching statement if ($data[$i][2]=~/$next/){ ..} and it always return false even if it's true in reality. I tried this if ($data[$i][2]=~/trnA\(tgc\)/){ ..}and it works. my question is : how to insert the '\' in front of each parenthesis into the variable $next?

4

2 回答 2

5

您需要引用元字符。

尝试这个。

print "match" if( $var1 =~ /\Q$var2\E/ );
于 2012-06-20T11:08:13.687 回答
4

我想你想要quotemeta

$next = "trna(tgc)";
$search = quotemeta($next);
if ($data[$i][2]=~/$search/){ 
    //..
}
于 2012-06-20T11:07:54.937 回答