我有一个变量$next,其中包含可能包含括号的字符串,例如trna(tgc)。我想要使用此匹配语句if ($data[$i][2]=~/$next/){ ..},即使实际上为真,它总是返回false。我尝试过这个if ($data[$i][2]=~/trnA\(tgc\)/){ ..}并且它可以工作。 我的问题是:如何在变量$next中在每个括号前插入'\'?
我认为您需要使用quotemeta函数: $next = "trna(tgc)"; $search = quotemeta($next); if ($data[$i][2]=~/$search/){ //.. }