Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Adding regex filters from command-line options

by ikegami (Patriarch)
on Mar 17, 2025 at 19:12 UTC ( [id://11164308]=note: print w/replies, xml ) Need Help??


in reply to Adding regex filters from command-line options

Use a closure which captures the pattern.

my @filters = ( ( map { my $re = $_; sub { $_[0] =~ $re } } @accept ), ( map { my $re = $_; sub { $_[0] !~ $re } } @reject ), );

You could even pre-compile the patterns.

my @filters = ( ( map { my $re = qr/$_/; sub { $_[0] =~ $re } } @accept ), ( map { my $re = qr/$_/; sub { $_[0] !~ $re } } @reject ), );

I suspect that building a single pattern would be faster, though.

my $filter; if ( @accept || @reject ) { $filter = join "", ( map "(?=(?s:.*?)$_)", @accept ), ( map "(?!(?s:.*?)$_)", @reject ); $filter = qr/^$filter/; } else { $filter = qr/(*FAIL)/; } sub filter { map $_->[0], grep { $_->[1] =~ $filter } @_ }

Replies are listed 'Best First'.
Re^2: Adding regex filters from command-line options
by Anonymous Monk on Mar 17, 2025 at 20:20 UTC
    Nice! But note the first two solutions require an extra set of enclosing parenthesis or the @reject filters will silently not be applied.

      woops! Fixed.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2026-02-18 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.