Use alpha assertions for more understandable regexes

[This feature stabilizes in Perl v5.32]

Perl v5.28 adds more-readable, alternate spelled-out forms for some of its regular expression extended patterns. Then, to make those slightly less readable, there are very short initialisms for those. Although these might seem superfluous now, the ability to define new syntax without relying on the limited number of ASCII symbols.

These are experimental features but you don’t need to enable them although you can turn off their warnings):

use v5.28;
no warnings qw(experimental::alpha_assertions);

You now have additional ways to specify the same things. I won’t explain these here, but I do cover lookarounds in Mastering Perl.

Feature Symbolic Alpha Assertion Shortcut
Positive lookahead (?=...) (*positive_lookahead:...) (*pla:...)
Negative lookahead (?!...) (*negative_lookahead:...) (*nla:...)
Positive lookbehind (?<=...) (*positive_lookbehind:...) (*plb:...)
Negative lookbehind (?<!...) (*negative_lookbehind:...) (*nlb:...)
Atomic (?>...) (*atomic:...)

Without the alpha assertion, you’d write a positive lookahead assertion. Here’s an example that finds “camel” and “fox” in a string without caring which order they appear:

$_          = 'The quick brown fox jumps over the lazy camel';
my $pattern = qr/(?=.*camel).*fox/;

say "Matched!" if /$pattern/;

With the alpha assertion, the pattern exchanges the symbols for letters:

use v5.28;
no warnings qw(experimental::alpha_assertions);

$_          = 'The quick brown fox jumps over the lazy camel';
my $pattern = qr/(*positive_lookahead:.*camel).*fox/;
# OR: my $pattern = qr/(*pla:.*camel).*fox/;

say "Matched!" if /$pattern/;

These make more sense with extended patterns so it’s easier to see the parts of the pattern (and to give room to the longer syntax):

use v5.28;
no warnings qw(experimental::alpha_assertions);

$_          = 'The quick brown fox jumps over the lazy camel';
my $pattern = qr/
	(*positive_lookahead:.*camel)
	.*fox
	/x;

say "Matched!" if /$pattern/;