Regular expression for not-group

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Lasher

    Regular expression for not-group

    Is it possible to write a regular expression such that a "match" is
    found provided the string does not match a group in the regex? Let me
    give a concrete example.

    Suppose I want to find a match to any filename that does not end in
    ..py, (ignoring the obvious use of the .endswith('.py' ) string method).
    I tried the things that were obvious to me, none of which worked.

    \.^(py)
    \.(^py)
    \.[^p][^y]

    The last one deceived me at first because it will match "spam.spam" ,
    but not "spam.parro t". I'm a bit stumped at this point. If this can be
    done with a regular expression, I'd love to know how, and even if it
    can't be, that would be very helpful to know, too.

    Many thanks in advance,
    Chris

  • Paddy

    #2
    Re: Regular expression for not-group


    Chris Lasher wrote:[color=blue]
    > Is it possible to write a regular expression such that a "match" is
    > found provided the string does not match a group in the regex? Let me
    > give a concrete example.
    >
    > Suppose I want to find a match to any filename that does not end in
    > .py, (ignoring the obvious use of the .endswith('.py' ) string method).
    > I tried the things that were obvious to me, none of which worked.
    >
    > \.^(py)
    > \.(^py)
    > \.[^p][^y]
    >
    > The last one deceived me at first because it will match "spam.spam" ,
    > but not "spam.parro t". I'm a bit stumped at this point. If this can be
    > done with a regular expression, I'd love to know how, and even if it
    > can't be, that would be very helpful to know, too.
    >[/color]

    The re module documentation has this snippet:

    (?!...)
    Matches if ... doesn't match next. This is a negative lookahead
    assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if
    it's not followed by 'Asimov'.

    - Paddy.

    Comment

    • Paddy

      #3
      Re: Regular expression for not-group


      P.S. kodos might help you: http://kodos.sourceforge.net/

      - Pad.

      Comment

      • Chris Lasher

        #4
        Re: Regular expression for not-group

        Man, that's a headslap-worthy overlooking of the obvious. Ha! =-)

        I was using the redemo.py that comes standard with Python but that
        Kodos app looks even neater! Thanks for the tip. Thanks Paddy.

        Chris

        Comment

        Working...