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

后端 未结 3 735
醉酒成梦
醉酒成梦 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 05:03

    You can use quotemeta (\Q \E) if your Perl is version 5.16 or later, but if below you can simply avoid using a regular expression at all.

    For example, by using the index command:

    if (index($text_to_search, $search_string) > -1) {
        print "wee";
    }
    

提交回复
热议问题