Regex matching a string that DOESN'T contain a given word

A

alan

I have a tough little nut to crack, and I'm no regexpert so I thought
I'd see if anyone out there had a solution.

I need to match strings that represent directory structures. I want
to know if I have a directory represented in my file that is
underneath directory a/b/c

However, I'm not interested if it's directory a/b/c/tests/...

so I need someway to filter that out of my matches.

But I have to do it in one regex expression because I'm forced to use
a grepping tool from within my application that takes a regular
expression.

Surely this isn't as hard as it seems like it is to me.

Can anyone help me with the pattern for this?

alan
 
P

Paul Lalli

alan said:
I have a tough little nut to crack, and I'm no regexpert so I thought
I'd see if anyone out there had a solution.

I need to match strings that represent directory structures. I want
to know if I have a directory represented in my file that is
underneath directory a/b/c

However, I'm not interested if it's directory a/b/c/tests/...

so I need someway to filter that out of my matches.

But I have to do it in one regex expression because I'm forced to use
a grepping tool from within my application that takes a regular
expression.

Surely this isn't as hard as it seems like it is to me.

Can anyone help me with the pattern for this?

This question pops up in this group every other month or so. I'm almost
surprised it's not a FAQ yet. The solution is to use a negative
lookahead assertion (read about them in perldoc perlre )

#!/usr/bin/perl
use strict;
use warnings;

while (<DATA>) {
print if /^(?!.*foobar)/;
}

__DATA__
This has foobar in it.
This does not.
foobar at the start.
ends with foobar

Output:
This does not.

That regexp is saying "Match the beginning of the string and then check
to see if 'foobar' does not follow anything in the string"

Hope this helps.
Paul Lalli
 
T

Tore Aursand

while (<DATA>) {
print if /^(?!.*foobar)/;
}

Why use a regular expression for this? Isn't it more convenient (at least
performance wise) to use the 'index' function?
 
D

David K. Wall

Why use a regular expression for this? Isn't it more convenient
(at least performance wise) to use the 'index' function?

If I understand the OP correctly, he isn't even using Perl:

"But I have to do it in one regex expression because I'm forced to use
a grepping tool from within my application that takes a regular
expression."
 
S

secret

I know it's a strange requirement. The problem is that I'm using 'ant' as
my build tool. Part of what it does is check cvs for new check-ins. If it
finds one I then want to determine where it is in the module to see what
needs rebuilding. So if the only checkins are in the test directory, I'll
just re-run the tests. If, however, there's a checkin that's not in the
tests dir then I have to rebuild everything and then run the tests.

This is a simplification of my task, but it's close enough.

I'm trying to avoid writing a custom ant task to do this work, so I'm using
a 'grep' task, which I had hoped would suffice but, if I understand your
posts, it won't.

alan
 
M

Michael Slass

secret said:
I know it's a strange requirement. The problem is that I'm using 'ant' as
my build tool. Part of what it does is check cvs for new check-ins. If it
finds one I then want to determine where it is in the module to see what
needs rebuilding. So if the only checkins are in the test directory, I'll
just re-run the tests. If, however, there's a checkin that's not in the
tests dir then I have to rebuild everything and then run the tests.

This is a simplification of my task, but it's close enough.

I'm trying to avoid writing a custom ant task to do this work, so I'm using
a 'grep' task, which I had hoped would suffice but, if I understand your
posts, it won't.

alan

Java's regular expressions, contained in java.util.regex, have
negative-lookahead, so maybe you can get what you want by writing a
path-matching class using those, and then calling it with the "java"
task. That beats shelling out to perl in that it runs in-proc with
ant (unless you specify the fork="true" attribute).
 
S

secret

That's the advice I was in need of!

Thanks Michael, negative lookahead is indeed my friend! And if this
question does make it into th FAQ then this is the answer I would have been
searching for. My problem was finding the right words to google on. I'm
sure this question has been answered before but I'm damned if I could find
it with my search words...

alan
 
M

Michael Slass

secret said:
That's the advice I was in need of!

Thanks Michael, negative lookahead is indeed my friend! And if this
question does make it into th FAQ then this is the answer I would have been
searching for. My problem was finding the right words to google on. I'm
sure this question has been answered before but I'm damned if I could find
it with my search words...

alan

That's great. I'd be curious to hear about your results; you can
email me directly since we're now pretty much OffTopic for a perl
group.

Best.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top