Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Regex on a variable with special characters

by haukex (Archbishop)
on Jul 22, 2016 at 08:47 UTC ( [id://1168320]=note: print w/replies, xml ) Need Help??


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
    This is exactly was I was looking for. Thank you.
Re^2: Regex on a variable with special characters
by Linicks (Scribe) on Jul 23, 2016 at 17:31 UTC

    Interesting - sometimes something easy becomes hard.

    I rememeber years ago (as a sysadmin) when upgrading the network machines, one machine couldn't get an IP address from the DHCP server (all MS stuff, of course). It turned out there was a bug in the MS stack in such that machine called 'stores_01' failed - the underscore fubarred it.

    So, keep the names sane, then the code can remain sane ~ and easy

    Nick

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1168320]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2024-04-23 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found