Perl regular [removed]using a variable as a search string with Perl operator characters included)

后端 未结 3 736
醉酒成梦
醉酒成梦 2020-11-30 04:40
$text_to_search = \"example text with [foo] and more\";
$search_string = \"[foo]\";

if ($text_to_search =~ m/$search_string/)
    print \"wee\";

P

3条回答
  •  旧巷少年郎
    2020-11-30 04:59

    Use the quotemeta function:

    $text_to_search = "example text with [foo] and more";
    $search_string = quotemeta "[foo]";
    
    print "wee" if ($text_to_search =~ /$search_string/);
    

提交回复
热议问题