Combine different case rules in one Perl regex

I have a Perl variable, $word . I want to make a regex as follows:

 $text =~ /ab($word)cd/; 

I want the regex to be case sensitive for the parts ab and cd , but not for what is in $word . Therefore, if $word='stack' , I would like both of them to match:

 abstackcd abStAcKcd 

etc., but I do not want to match

 Abstackcd 

I suppose I'm looking for a way to apply /i only to $word , but not the rest of the expression. It can be done?

+6
source share
1 answer

Yes, using (?i:$word) . See the Advanced Templates section of perldoc perlre . Perhaps you really wanted to (?i:\Q$word\E) , by the way, that it would automatically quote any regular expression metacharacters that are in $word .

+15
source

Source: https://habr.com/ru/post/914671/


All Articles