in reply to Regex on a variable with special characters
Hi jaydee,
$y =~ /^\Q$x\E/;
See also quotemeta and Mind the meta!
Update: Actually, you said "$y sometimes contains regex special characters" - my answer applies if $x contains special characters. If $y contains special characters and $x does not, you don't necessarily need to escape $x, although the general advice in Mind the meta! still applies that it's safer to always use \Q...\E except in the case that you intentionally want the contents of the variable being interpolated into the regex to be interpreted as a regex. Update 2: Clarified wording.
Hope this helps,
-- Hauke D
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex on a variable with special characters
by jaydee (Initiate) on Jul 22, 2016 at 09:02 UTC | |
|
Re^2: Regex on a variable with special characters
by Linicks (Scribe) on Jul 23, 2016 at 17:31 UTC |