How to use the case-insensitive modifier /i in Perl regex?
Question
How to use the case-insensitive modifier /i in Perl regex?
Using the Case-Insensitive Modifier /i in Perl Regular Expressions
In Perl, regular expressions (regex) are powerful tools for pattern matching and text manipulation. Sometimes, you may want to perform matches without worrying about letter casing (uppercase vs lowercase). Perl provides the /i modifier to make your regex case-insensitive, meaning it treats uppercase and lowercase letters as equivalent.
What is the /i Modifier?
The /i modifier is appended after the regex pattern delimiter, and it tells Perl to ignore letter case when matching letters:
/foo/iwill matchfoo,FoO,FOO, orfOo.- Without
/i, the regex is case-sensitive by default, so/foo/matches onlyfooexactly.
Thus, /i is very useful when user input or data may use inconsistent capitalization, but you want a flexible match.
Example: Matching Case-Insensitive Words
Here is a complete Perl script demonstrating the /i modifier in action. It checks various strings for the presence of the word "Perl", regardless of case:
#!/usr/bin/perl
use strict;
use warnings;
my @test_strings = (
"I love Perl programming.",
"perl is great!",
"PERL is powerful.",
"Python is different.",
);
foreach my $str (@test_strings) {
if ($str =~ /perl/i) {
print qq|'$str' contains 'perl' (case-insensitive match)\n|;
} else {
print qq|'$str' does NOT contain 'perl'\n|;
}
}
Output:
'I love Perl programming.' contains 'perl' (case-insensitive match) 'perl is great!' contains 'perl' (case-insensitive match) 'PERL is powerful.' contains 'perl' (case-insensitive match) 'Python is different.' does NOT contain 'perl'
How It Works
- The regex
/perl/imatches the substring "perl" in any case form. - The
=~operator tests whether the string on the left matches the regex on the right. - If a match occurs, the
ifblock runs; otherwise, theelseblock runs.
Perl-Specific Concepts
- Sigils: The
=~operator links a scalar variable (here,$str) to a regex match. - Modifiers: The
/iis one of several regex modifiers (others include/gfor global match and/mfor multiline). - Context: Regex matches in scalar context return a boolean indicating success/failure of matching.
- TMTOWTDI: "There’s more than one way to do it" — alternatively, you could use
lc($str)and a lowercase regex, but the/imodifier is more concise and idiomatic.
Common Gotchas
- Non-letter characters: The
/imodifier only affects ASCII letters. Special Unicode case folding may require extra care or the/iwith Unicode regex features (use utf8;). - Performance: The
/imodifier adds slight overhead — avoid using it unnecessarily in tight loops. - Mixing cases: If you capture groups, remember the matched text preserves the original case, only matching is case-insensitive.
Summary
The /i modifier in Perl regex makes pattern matching case-insensitive, allowing you to match letters regardless of case. It is easy to use — just add i after the regex delimiters, like /pattern/i. This is one of Perl’s most useful regex features and helps make text processing more flexible and robust.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 7ms
'I love Perl programming.' contains 'perl' (case-insensitive match)
'perl is great!' contains 'perl' (case-insensitive match)
'PERL is powerful.' contains 'perl' (case-insensitive match)
'Python is different.' does NOT contain 'perl'
(empty)Was this helpful?
Related Questions
- How to validate email addresses using Perl regex?
- How to split a string by regex pattern in Perl?
- How to use greedy vs non-greedy quantifiers in Perl regex?
- How to match start and end of string with ^ and $ in Perl?
- How to use alternation | in Perl regex?
- How to use the qr// operator to compile regex in Perl?