itzy
itzy

Reputation: 11755

Combine different case rules in one Perl regular expression

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?

Upvotes: 6

Views: 138

Answers (1)

hobbs
hobbs

Reputation: 239861

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.

Upvotes: 15

Related Questions