Regex Modifiers

Learn about modifiers that change the behavior of the regex expression operators.

The /i modifier

Several modifiers change the behavior of the regular expression operators. These modifiers appear at the end of the match, substitution, and qr// operators. For example, here’s how to enable case-insensitive matching:

Press + to interact
use Test::More;
my $pet = 'ELLie';
like $pet, qr/Ellie/, 'Nice puppy!';
like $pet, qr/Ellie/i, 'shift key br0ken';
done_testing();

The first like() will fail because the strings contain different letters. The second like() will pass, because the /i modifier causes the regex to ignore case distinctions. L and l are effectively equivalent in the second regex due to the modifier.

Embedding regex modifiers within patterns

We can also embed regex modifiers within a pattern:

Press + to interact
my $find_a_cat = qr/(?<feline>(?i)cat)/;

The (?i) syntax enables case-insensitive matching only for its ...