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 (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