💾 Archived View for thrig.me › blog › 2022 › 11 › 17 › regular-expression-alternation.gmi captured on 2023-06-16 at 16:52:31. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-05-24)

➡️ Next capture (2023-11-14)

-=-=-=-=-=-=-

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

bphflog links

bphflog index

next: Pushing Pieces