regex intermediate

How to use the /x modifier for readable regex in Perl?

Question

How to use the /x modifier for readable regex in Perl?

Using the /x Modifier for Readable Regular Expressions in Perl

The /x modifier in Perl allows you to write regular expressions with whitespace and comments to improve readability without affecting the pattern matching. It stands for “extended” mode and is especially helpful for complex regexes that are hard to decipher in a single compressed line.

How /x works

  • Whitespace in the pattern is ignored, except when escaped (\\ ) or inside a character class ([ ]).
  • You can add comments preceded by # which extend to the end of the line.
  • The regex engine ignores these whitespace and comments, so the logical pattern stays the same, but your code becomes much easier to understand.

The /x modifier is valuable because Perl regexes can get dense fast. Using /x lets you space out components, align related parts, and add inline explanations.

Example: Matching a U.S. Phone Number with /x


use strict;
use warnings;

my $text = "Call me at (123) 456-7890 or 987-654-3210.";

# Regex to match U.S. phone numbers in various formats
# Using /x for readability
my $phone_regex = qr{
    \(?          # Optional opening parenthesis
    (\d{3})      # Area code (3 digits)
    \)?          # Optional closing parenthesis
    \s*          # Optional whitespace
    [-.]?        # Optional separator (dash or dot)
    \s*          # Optional whitespace
    (\d{3})      # First 3 digits
    [-.]?        # Optional separator
    \s*          # Optional whitespace
    (\d{4})      # Last 4 digits
}x;

while ($text =~ /$phone_regex/g) {
    print "Found phone number: $1-$2-$3\n";
}

Explanation

  • qr{...}x defines the regex with /x for extended mode.
  • Whitespace and comments inside the regex pattern do not affect matching.
  • We can split the pattern over multiple lines, add comments after #, and space it out logically.
  • Parentheses capture parts of the phone number, which are printed in a normalized format.

Perl-specific Notes

  • Be careful that literal whitespace you want to match must be escaped or inside character classes. For example, \s for spaces or [ ] for matching a space character.
  • Comments (#) only act as comments outside character classes and must not appear in the middle of tokens unless intended.
  • Since /x ignores spaces, you cannot blindly spread out parts of anchors like ^ or $ next to other tokens without care.
  • If you want to combine /x with other modifiers, simply append them together, e.g., /ix for case-insensitive and extended mode.

Common Pitfalls

  • Forgetting to escape literal spaces that matter, leading to a regex that matches differently than expected.
  • Using # inside character classes thinking it becomes a comment (it does not).
  • Mixing /x with negated character classes or unusual regex features that expect exact spacing.
  • Expecting comments inside multi-line strings passed to qr// without the /x modifier to be ignored (they will not).

In summary, /x is your friend for building maintainable regexes in Perl. It embodies Perl’s philosophy of "There's more than one way to do it" (TMTOWTDI) by letting you choose more human-friendly layouts while keeping the power of Perl’s regex engine.

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
Found phone number: 123-456-7890
Found phone number: 987-654-3210
STDERR
(empty)

Was this helpful?

Related Questions