regex beginner

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/i will match foo, FoO, FOO, or fOo.
  • Without /i, the regex is case-sensitive by default, so /foo/ matches only foo exactly.

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/i matches 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 if block runs; otherwise, the else block runs.

Perl-Specific Concepts

  • Sigils: The =~ operator links a scalar variable (here, $str) to a regex match.
  • Modifiers: The /i is one of several regex modifiers (others include /g for global match and /m for 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 /i modifier is more concise and idiomatic.

Common Gotchas

  • Non-letter characters: The /i modifier only affects ASCII letters. Special Unicode case folding may require extra care or the /i with Unicode regex features (use utf8;).
  • Performance: The /i modifier 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

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
'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'
STDERR
(empty)

Was this helpful?

Related Questions