negate regex?

116 views
Skip to first unread message

Daniel_P_Kirkwood

unread,
Jul 19, 1999, 3:00:00 AM7/19/99
to
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..

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...

Tom Christiansen

unread,
Jul 19, 1999, 3:00:00 AM7/19/99
to
[courtesy cc of this posting mailed to cited author]

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

Eric Bohlman

unread,
Jul 20, 1999, 3:00:00 AM7/20/99
to
Daniel_P_Kirkwood (dan...@lucent.com) wrote:
: 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...

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.


Daniel_P_Kirkwood

unread,
Jul 20, 1999, 3:00:00 AM7/20/99
to
Tom, thanx for the hint.. I looked in the Ram book, FAQs, etc before
posting, but somehow I missed that one.. -dan
Reply all
Reply to author
Forward
0 new messages