Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine different case rules in one Perl regular expression

Tags:

regex

perl

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?

like image 296
itzy Avatar asked May 01 '12 20:05

itzy


1 Answers

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.

like image 74
hobbs Avatar answered Oct 12 '22 11:10

hobbs