Regular Expression Alternation

    my $command = qr/(ab)/;

Business needs might prompt the addition of longer command names, so instead of just ab one might add:

    my $command = qr/(ab|abbr|abbreviate)/;

This is buggy; the regular expression will never match abbr nor abbreviate.

    $ aslines ab abbr abbreviate | perl -nE 'say $1 if /(ab|abbr|abbreviate)/'
    ab
    ab
    ab

Causes of this bug are where the alternation is thrown together at random--and never tested nor reviewed, an all too common case--or where software automatically builds the alternation and that building software is buggy. The Data::Munge Perl module by contrast takes a number pains in the list2re function to get this right.

=> https://metacpan.org/pod/Data::Munge

Other software or people writing software may be less enlightened about such matters. Buyer beware?

tags #regex #perl

Proxy Information
Original URL
gemini://thrig.me/blog/2022/11/17/regular-expression-alternation.gmi
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
1003.191466 milliseconds
Gemini-to-HTML Time
0.28436 milliseconds

This content has been proxied by September (ba2dc).