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

Discussion in 'Perl Misc' started by alan, Aug 31, 2004.

  1. alan

    alan Guest

    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
     
    alan, Aug 31, 2004
    #1
    1. Advertisements

  2. alan

    Paul Lalli Guest

    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
     
    Paul Lalli, Aug 31, 2004
    #2
    1. Advertisements

  3. alan

    Tore Aursand Guest

    Why use a regular expression for this? Isn't it more convenient (at least
    performance wise) to use the 'index' function?
     
    Tore Aursand, Aug 31, 2004
    #3
  4. 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."
     
    David K. Wall, Aug 31, 2004
    #4
  5. alan

    secret Guest

    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
     
    secret, Sep 1, 2004
    #5
  6. 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).
     
    Michael Slass, Sep 1, 2004
    #6
  7. alan

    secret Guest

    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
     
    secret, Sep 1, 2004
    #7
  8. 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.
     
    Michael Slass, Sep 2, 2004
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.