Perl Regular Expressions: Still the Gold Standard
Perl's regex engine remains the most powerful text processing tool available. Here are patterns worth knowing.
Key Insights
- Named captures (?
…) make complex patterns self-documenting - Non-greedy quantifiers (*?, +?) prevent over-matching in nested structures
- The /x flag allows whitespace and comments inside patterns for readability
Named Captures
my $log = '2024-01-15 ERROR [auth] Login failed for user@example.com';
if ($log =~ /(?<date>\d{4}-\d{2}-\d{2})\s+(?<level>\w+)\s+\[(?<module>\w+)\]\s+(?<msg>.+)/) {
say "Module: $+{module}, Level: $+{level}";
}
Extended Mode for Readability
my $email_re = qr/
(?<user> [a-zA-Z0-9._%+-]+) # local part
@
(?<domain> [a-zA-Z0-9.-]+) # domain
\.
(?<tld> [a-zA-Z]{2,}) # top-level domain
/x;
Lookahead and Lookbehind
# Password: at least one digit and one uppercase, 8+ chars
my $valid = $password =~ /^(?=.*\d)(?=.*[A-Z]).{8,}$/;