I want to be able to store multiple regex's in a list, for example, and
have them applied to a list of files, but I want each of the regex's to
be able to say whether it's to be applied using =~ or !~ without extra
work by the driving program..
Here's a contrived example: @filters = ( qr/^foo/, !qr/bar/, qr/baz/)
Meaning, I want everything in my list that starts with foo, then
everything that doesn't have bar, etc.. Obviously, this isn't correct
syntax, but you get my meaning...
Any help would be greatly appreciated...
In comp.lang.perl.misc, Daniel_P_Kirkwood <dan...@lucent.com> writes:
:Is there a way in a regex to negate the result ala !~ or "grep -v"? I've
:looked at looking at negative assertions, but they're not general
:enough..
:
:I want to be able to store multiple regex's in a list, for example, and
:have them applied to a list of files, but I want each of the regex's to
:be able to say whether it's to be applied using =~ or !~ without extra
:work by the driving program..
From Chapter Six of the Ram Book:
True if either C</ALPHA/> or C</BETA/> matches, like
C</ALPHA/ || /BETA/>:
/ALPHA|BETA/
True if both C</ALPHA/> and C</BETA/> match, but may overlap,
meaning that C<"BETALPHA"> should be ok, like C</ALPHA/ && /BETA/>:
/^(?=.*ALPHA)(?=.*BETA)/s
True if both C</ALPHA/> and C</BETA/> match, but may not overlap,
meaning that C<"BETALPHA"> should fail:
/ALPHA.*BETA|BETA.*ALPHA/s
True if pattern C</PAT/> does not match, like C<$var !~ /PAT/>:
/^(?:(?!PAT).)*$/s
True if pattern C<BAD> does not match, but pattern C<GOOD> does:
/(?=^(?:(?!BAD).)*$)GOOD/s
--tom
--
Adding manpower to a late software project makes it later
You're probably better off storing them as anonymous subs, e.g.
@filters=(sub {/foo/}, sub {!/bar}, sub {/baz/});
This assumes that all the matches are against $_; if not, you'll need to
wire in the appropriate variable name.