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

  2. Gunnar Hjalmarsson, Apr 28, 2004
    #2
    1. Advertising

  3. Hal Vaughan

    Paul Lalli Guest

    On Wed, 28 Apr 2004, Hal Vaughan wrote:

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



    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

    On Wed, 28 Apr 2004 00:40:21 GMT, Hal Vaughan <> wrote:
    > 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}/


    That will just match everything, after all all strings contain a substring
    (of zero length) containing no copies of 'web-SL0'.
    >
    > 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?


    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
     
    Sam Holden, Apr 28, 2004
    #4
  5. Hal Vaughan

    Bob Walton Guest

    Hal Vaughan wrote:

    ....


    > "-server-SL0". How can I specify, in a regex, to NOT match "web-SL0", but
    > to match all else?

    ....


    > my references, but I can't find a way to specify to NOT match a pattern in
    > a regex. Is it possible?

    ....


    > Hal


    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
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Apr 28, 2004
    #5
  6. Hal Vaughan

    Hal Vaughan Guest

    Sam Holden wrote:

    > On Wed, 28 Apr 2004 00:40:21 GMT, Hal Vaughan <>
    > wrote:
    >> 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}/

    >
    > That will just match everything, after all all strings contain a substring
    > (of zero length) containing no copies of 'web-SL0'.
    >>
    >> 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?

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


    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
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 28, 2004
    #7
  8. Hal Vaughan

    Hal Vaughan Guest

    Gunnar Hjalmarsson wrote:

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


    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:

    > Hal Vaughan wrote:
    >
    > (snipped)
    >
    >> 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?

    >
    > if (index ($input, "-server-SL0") > -1)
    > { next; }


    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
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Apr 28, 2004
    #9
  10. Hal Vaughan wrote:
    > Gunnar Hjalmarsson wrote:
    >> 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?

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


    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.

    > The holes in one's experience if you're self-taught can be very
    > frustrating, at times.


    Yeah, indeed they can. ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 28, 2004
    #10
  11. Hal Vaughan

    Hal Vaughan Guest

    Gunnar Hjalmarsson wrote:

    > Hal Vaughan wrote:
    >> Gunnar Hjalmarsson wrote:
    >>> 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?

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

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


    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

    >> The holes in one's experience if you're self-taught can be very
    >> frustrating, at times.

    >
    > Yeah, indeed they can. ;-)
    >
     
    Hal Vaughan, Apr 28, 2004
    #11
  12. Hal Vaughan

    Hal Vaughan Guest

    Purl Gurl wrote:

    > Hal Vaughan wrote:
    >
    > (snipped)
    >
    >> 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?

    >
    > if (index ($input, "-server-SL0") > -1)
    > { next; }
    >
    >
    > Purl Gurl


    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. In article <gHDjc.31668$0u6.5673561@attbi_s03>,
    Hal Vaughan <> wrote:

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


    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

    --
    Pukku
     
    Richard Morse, Apr 28, 2004
    #13
  14. Hal Vaughan

    Hal Vaughan Guest

    Purl Gurl wrote:

    > Tassilo v. Parseval wrote:
    >
    >> Purl Gurl wrote:
    >> > Hal Vaughan wrote:

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

    >
    > Irrelevant. Your comments do not comply with the
    > originating author's stated parameters.


    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

    >
    > Purl Gurl
     
    Hal Vaughan, Apr 29, 2004
    #14
  15. Hal Vaughan

    Hal Vaughan Guest

    Purl Gurl wrote:

    > Hal Vaughan wrote:
    >
    >> Purl Gurl wrote:
    >> > Hal Vaughan wrote:

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

    >
    > Please provide a source reference for this rule
    > which states alternative code cannot used.


    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.

    > You lack imagination.


    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

    >
    > Purl Gurl
     
    Hal Vaughan, Apr 29, 2004
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    1
    Views:
    954
    Ilias Lazaridis
    Sep 22, 2006
  2. Xah Lee
    Replies:
    8
    Views:
    465
    Ilias Lazaridis
    Sep 26, 2006
  3. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,309
    Tim Rentsch
    Sep 23, 2005
  4. Hongyu
    Replies:
    9
    Views:
    927
    James Kanze
    Aug 8, 2008
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    247
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page