Perl::...matting(3pm)
π Perl::Critic::Policy::RegularExpressions::RequireExtendedFormatting (3pm) β¨
okay so bestie, you've stumbled into the User Contributed Perl Documentation corner of the internet, and this page is literally about one tiny thing: making your regexes readable. slay.
π NAME π
Perl::Critic::Policy::RegularExpressions::RequireExtendedFormatting β bestie, this policy is begging you to always use the /x modifier with your regular expressions. no cap. π
πΈ AFFILIATION πΈ
This policy is part of the core Perl::Critic distribution. it ships with the whole crew, iykyk. π
π¦ DESCRIPTION π¦
okay so here's the tea β β extended regular expression formatting lets you mix whitespace and comments right into the pattern, and that makes them so much more readable. it's giving glow-up. it's giving self-care. π
look at this mess first:
# Match a single-quoted string efficiently...
m{'[^\\']*(?:\\.[^\\']*)*'}; #Huh?
# Same thing with extended format...
m{
' # an opening single quote
[^\\'] # any non-special chars (i.e. not backslash or single quote)
(?: # then all of...
\\ . # any explicitly backslashed char
[^\\']* # followed by a non-special char
)* # ...repeated zero or more times
' # a closing single quote
}x;
see?? same regex, but the second one actually explains itself. main character energy fr fr. π
β¨ CONFIGURATION β¨
now, bestie, sometimes you'll be like "girl, a /x on a short regex is doing the most." and honestly? fair. π so there's an escape hatch:
set minimum_regex_length_to_complain_about to the minimum match length you'll allow without a /x. important detail: the length only counts the regular expression itself β not the braces, not the operators. got it? π«Ά
[RegularExpressions::RequireExtendedFormatting]
minimum_regex_length_to_complain_about = 5
$num =~ m<(\d+)>; # ok, only 5 characters
$num =~ m<\d\.(\d+)>; # not ok, 9 characters
this option defaults to 0. β
next up: because slapping /x on a regex that already has whitespace in it can make it harder to read (you'd have to escape all that innocent whitespace π), the default is that you're allowed to have a regular expression containing only whitespace and word characters without the modifier. it's fine. it's chill.
but if you want to restrict that and be strict about it, turn on the strict option:
[RegularExpressions::RequireExtendedFormatting]
strict = 1
$string =~ m/Basset hounds got long ears/; # no longer ok
this option defaults to false. π
π₯ NOTES π₯
for common regular expressions like e-mail addresses, phone numbers, dates, etc., go have a look at the Regexp::Common module β it's iconic and it'll save you.
β οΈ also, be cautious about slapping modifier flags onto existing regular expressions, because they can drastically alter their meaning. like, not "oops typo" altered, "your code does something completely different now" altered. π
if you want an interesting discussion on the effects of blindly modifying regular expression flags, see <http://www.perlmonks.org/?node_id=484238>. π§
π TO DO π
the plan (future era): add an exemption for regular expressions that contain \Q at the front and don't use \E until the very end, if at all. noting it here so nobody forgets. π
π AUTHOR π
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
π COPYRIGHT π
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.
okay that's the whole page, bestie π mwah π β this was Perl::...matting(3pm) from perl v5.42.0, dated 2024-10-23, brought to you by User Contributed Perl Documentation. go forth and /x your regexes. periodt. β¨π¦
π¦ where this page comes from
- opensuse-leap 16.0: perl-Perl-Critic 1.156.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms