I have a Perl variable, $word
. I want to do a regex like this:
$text =~ /ab($word)cd/;
I want the regex to be case-sensitive for the ab
and cd
parts, but not for whatever is in $word
. So if $word='stack'
, I would want both of these to match:
abstackcd
abStAcKcd
etc., but I don't want to match
Abstackcd
I guess I'm looking for some way to apply the /i
just to $word
but not the rest of the expression. Can this be done?
Yes, using (?i:$word)
. See the section "Extended Patterns" of perldoc perlre. You may have actually wanted (?i:\Q$word\E)
, by the way, which will automatically quote any regex metacharacters that are in $word
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With