I have a string $regexp_as_string
Now I want to “convert” it into an regex / use it as regexp
if ($text_to_search =~ $regexp_as_string)
{
...
}
Now there are characters like “.” and I want to automaticly escape them – \Q and \E should do that
if ($text_to_search =~ /\Q$regexp_as_string\E/)
{
...
}
Is there a way to specify a list of characters that should be auto escaped? Because at the moment that way auto escapes for example “|” , but I want to keep that.
You can prepare the string using
quotemeta, then remove backslashes selectively. E.g.:Output:
Add any characters you want not escaped to the character class in the substitution, e.g.
[|?()].