in reply to Q regex escape within variable

\Q interpretation is not done by the regex engine, but by string interpolation. Except you add the \Q in a single quote string, so the interpolation does not happen, and the \Q and \E are still there for the regex engine to read, though they don't have meaning in a regex. If you want to pre-process a string so that its special chars are excaped, you can use quotemeta, which is what \Q does implicitly. my $pref = "^".quotemeta($1);

Replies are listed 'Best First'.
Re^2: Q regex escape within variable
by palkia (Monk) on Mar 28, 2017 at 09:37 UTC
    Hello Eily.
    Thank you very much for your solution, it seems to be working perfectly after I adapted $pref =~ s/\d++/\\E\\d++\\Q/g; to $pref =~ s/\d++/\\d++/g;
    This would have most likely taken me long to figure out, I'm happy I didn't struggle through it for long before asking for help (like I usually would ^^).
    Thx again :)