Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Archive::Zip matching different file extension

by haukex (Archbishop)
on Apr 13, 2021 at 15:32 UTC ( [id://11131195]=note: print w/replies, xml ) Need Help??


in reply to XML::XPath matching different file extension

I assume you meant Archive::Zip instead of XML::XPath? At least according to the documentation, this should work: my @files = $zip->membersMatching( qr/\.(?:txt|png)$/i );

Replies are listed 'Best First'.
Re^2: Archive::Zip matching different file extension
by Anonymous Monk on Apr 13, 2021 at 15:58 UTC
    Thank you!!
      FYI, in the Perl world it is very common to find that – for your convenience – a library will accept either a string-pattern or a regular expression as an argument. Regular expressions, as haukex just demonstrated, are by far the most powerful alternative.
        ... 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 the Perl world it is very common to find that

        Too much of a generalization. One should always read the documentation as to the module's API.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131195]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found