... it is very common to find that ... a library will accept either a string-pattern or a regular expression .... Regular expressions ... are by far the most powerful alternative.

Strings and "regular expressions" (the Regexp object form produced by the qr// constructor) are both compiled to the same internal representation when used by a matching or match binding operator.

Win8 Strawberry 5.8.9.5 (32) Thu 04/15/2021 1:56:15 C:\@Work\Perl\monks >perl use strict; use warnings; use Test::More 'tests' => 2; 1..2 use Test::NoWarnings; my $rqr = qr/\.(?:txt|png)$/i; # regex as Regexp object my $rsq = '(?i)\.(?:txt|png)$'; # regex as single-quoted string my @Inputs = qw( x.txt .txt x.png .png x.TxT .tXt x.pNg .PnG txt png .txtx .pngx foo xtxtx ); my @mqr = grep { $_ =~ $rqr } @Inputs; # matches from qr regex my @msq = grep { $_ =~ $rsq } @Inputs; # matches from '' regex print "qr matches: @mqr \n"; is_deeply \@mqr, \@msq, 'qr// vs q//'; ^Z qr matches: x.txt .txt x.png .png x.TxT .tXt x.pNg .PnG ok 1 - qr// vs q// ok 2 - no warnings
There is no qr// expression for which an equivalent q// expression cannot (fairly easily) be written.

That said, it's always best IMHO to write regexes as qr// expressions. Doing so avoids many subtle (and a few blatant) bugs having to do with differences in escaping and interpolation between single/double-quoted string constructors and the qr// constructor, and quantification is better-behaved:

Win8 Strawberry 5.8.9.5 (32) Thu 04/15/2021 2:32:48 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $rqr = qr/foo/; my $rsq = q/foo/; my $s = 'foofoofoo'; print "$rqr match: '$1' \n" if $s =~ m{ ($rqr+) }xms; print "'$rsq' match: '$1' \n" if $s =~ m{ ($rsq+) }xms; ^Z (?-xism:foo) match: 'foofoofoo' 'foo' match: 'foo'
I find it a neat syntactic feature that in $rqr? $rqr* $rqr+ $rqr{n} $rqr{n,m} expressions, the Regexp object is quantified atomically. This allows regexes to be factored in a reasonable way.

(It should be noted that split has a number of special-case interpretations of its PATTERN argument (update: and some of these differentiate between string and Regexp representations).)


Give a man a fish:  <%-{-{-{-<


In reply to Re^4: Archive::Zip matching different file extension by AnomalousMonk
in thread XML::XPath matching different file extension by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.