Regex Matching Strings WITHOUT Chars

Discussion in 'Perl Misc' started by Hal Vaughan, Apr 28, 2004.

  1. Hal Vaughan

    Hal Vaughan Guest

    I'm using Archive::Zip and I can specify, with a regex, the filenames that
    are added to an archive. I have two types of log files, distinguishable by
    name types. One ends with "-web-SL0" and the other ends with
    "-server-SL0". How can I specify, in a regex, to NOT match "web-SL0", but
    to match all else?

    I tried a few ideas, like specifying to match it 0 times:

    /(web-SL0){0}/

    but it didn't work. I also tried various combinations, like match all chars
    OR zero occurances of web-SL0, but nothing worked. I've looked through all
    my references, but I can't find a way to specify to NOT match a pattern in
    a regex. Is it possible?

    Thanks!

    Hal
     
    Hal Vaughan, Apr 28, 2004
    #1
    1. Advertisements

  2. !/web-SL0$/
     
    Gunnar Hjalmarsson, Apr 28, 2004
    #2
    1. Advertisements

  3. Hal Vaughan

    Paul Lalli Guest


    There's a few ways. In this case, I'd say a negative lookbehind assertion
    is your best bet:

    /(?<!web-SL0)$/

    This says "Match the end of the string so long as it is NOT preceeded by
    "web-SL0".

    You can read up on this feature by searching for 'look-behind' in
    perldoc perlre

    Hope this helps,
    Paul Lalli
     
    Paul Lalli, Apr 28, 2004
    #3
  4. Hal Vaughan

    Sam Holden Guest

    That will just match everything, after all all strings contain a substring
    (of zero length) containing no copies of 'web-SL0'.
    If the "end with" statement in the description is correct then:

    /(?<!-web-SL0)$/

    See the
    perldoc perlre
    documentation - search for "look-behind" for the description of the syntax.
     
    Sam Holden, Apr 28, 2004
    #4
  5. Hal Vaughan

    Bob Walton Guest

    Hal Vaughan wrote:

    ....

    Check out:

    perldoc perlre

    in particular looking for text that says "zero-width negative look-ahead
    assertion" and "zero-width negative look-behind assertion". By properly
    applying one of those, you should be able to accomplish your task.
     
    Bob Walton, Apr 28, 2004
    #5
  6. Hal Vaughan

    Hal Vaughan Guest

    Thanks. I also got a private reply suggesting the same topic.  I had not
    tried perlre -- tried perldoc -q regex, and variations on
    perldoc regex, re, or "regular expression", but perlre has stuff the others
    don't.  I had not even realized there would be more info specifically for
    perlre instead of regex or re -- just never thougth to add "perl" to it!

    That'll do it.

    Hal
     
    Hal Vaughan, Apr 28, 2004
    #6
  7. I don't understand why everyone (except me) suggest solutions with
    extended regex patterns. It may be the most accurate answer to OP's
    literal question, but don't you forget the context? You don't need any
    extended pattern to exclude filenames matching a pattern when
    archiving files, do you? And isn't !/string/ an easier solution that
    would be preferable in this case?
     
    Gunnar Hjalmarsson, Apr 28, 2004
    #7
  8. Hal Vaughan

    Hal Vaughan Guest

    Actually, your solution is much simpler (as to which is faster, I would not
    know). But, as an additional point, I am self taught, and learning about
    look-behinds will be helpful, not just for this context, but in other
    contexts.

    In this particular case, using !/string/ was simpler and what I used in for
    this particular problem. However, there are some other problems I'm
    working on with regexes, and for those, the look-behind will be a HUGE help
    for me.

    I also had missed a helpful topic in Perldoc, and the public responses
    pointed that out (and a private response suggested something so obvious
    that I had never thought of it -- to use "perldoc perldoc"). The holes in
    one's experience if you're self-taught can be very frustrating, at times.

    Hal
     
    Hal Vaughan, Apr 28, 2004
    #8
  9. Also sprach Purl Gurl:
    Your solution will also identify those files where "-server-SL0" shows
    up somewhere in the middle of the string.

    According to you, your solutions are always obeying the specifications.
    Here however, it mysteriously doesn't.

    Tassilo
     
    Tassilo v. Parseval, Apr 28, 2004
    #9
  10. Okay, Hal, it's good that you (and others) will benefit from their
    mentioning of extended patterns. It's just that people who answer here
    typically take great pains in finding the most suitable method for
    addressing OP's question, and I began to wonder if I had missed
    something. But since nobody has claimed that extended patterns are
    necessary, I suppose not.
    Yeah, indeed they can. ;-)
     
    Gunnar Hjalmarsson, Apr 28, 2004
    #10
  11. Hal Vaughan

    Hal Vaughan Guest

    That's a very good point. In this case, I'm quite lucky, because both
    responses helped me. But you make a valid point. Perl programmers seem to
    be a special breed, and they're definitely hackers. To that mindset, the
    more unique and obscure a piece of code is, the more interesting it is,
    and, therefore, the better it is.

    But I can't complain -- like I said, both answers were a HUGE help to me.

    Hal
     
    Hal Vaughan, Apr 28, 2004
    #11
  12. Hal Vaughan

    Hal Vaughan Guest

    Nope, not at all.

    Forgot the regex part (in the original post and question -- and even in the
    title) in your eagerness?

    Hal
     
    Hal Vaughan, Apr 28, 2004
    #12
  13. If you type 'perldoc perl', you will get a list of all the various
    included docs, where you can find perlrequick, perlretut, perlfaq6, and
    perlre, among many others...

    HTH,
    Ricky
     
    Richard Morse, Apr 28, 2004
    #13
  14. Hal Vaughan

    Hal Vaughan Guest

    Nope. He is right on target. You are irrelevant. You are just to busy
    blaming everyone else to notice that there's a difference between showing
    off and helping a poster.

    On my old system I had a filter in place to kill Purl Gurl posts, since I
    found they were 1) always sassy, 2) focused on showing off, 3) concerned
    with making Purl Gurl look good by making others look bad, 4) often
    irrelevant because it was more important for Purl Gurl to look good than to
    help.

    Time to set up that filter again.

    Hal
     
    Hal Vaughan, Apr 29, 2004
    #14
  15. Hal Vaughan

    Hal Vaughan Guest

    I stated the needs, and said I could specify with a regex. I did not say
    with a regex or something else. Do you read posts completely before
    answering or do you just post quickly, to show off, without regard to
    whether your post is actually helpful? What is your honest goal? To show
    off, to look good, to impress people, or to help them? Your posts indicate
    it is the first 3 and not the last one.
    Nope, you lack the ability to pay attention to the parameters presented and
    are more interested in looking good than helping others.

    (That's a funny thing to say to someone who used to deal with the producers
    of a sci-fi tv show and had story ideas turned down because they went too
    far beyond the "normal" mold....)

    Hal
     
    Hal Vaughan, Apr 29, 2004
    #15
    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.