$text_to_search = \"example text with [foo] and more\";
$search_string = \"[foo]\";
if ($text_to_search =~ m/$search_string/)
print \"wee\";
P
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/);
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";
}
Use \Q
to autoescape any potentially problematic characters in your variable.
if($text_to_search =~ m/\Q$search_string/) print "wee";