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{...}xdefines the regex with/xfor 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,
\sfor 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
/xignores spaces, you cannot blindly spread out parts of anchors like^or$next to other tokens without care. - If you want to combine
/xwith other modifiers, simply append them together, e.g.,/ixfor 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
/xwith negated character classes or unusual regex features that expect exact spacing. - Expecting comments inside multi-line strings passed to
qr//without the/xmodifier 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
Found phone number: 123-456-7890
Found phone number: 987-654-3210
(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?