Regular expression for not-group

adam johnson adam.sven.johnson at gmail.com
Thu Jun 15 17:19:48 EDT 2006


You want to use negative lookahead eg.

\.(?!py)

it matches only if the characters ahead in the regex don't match the pattern
in the brackets.

**http://docs.python.org/lib/re-syntax.html (about halfway down the page)

On 15 Jun 2006 14:11:39 -0700, Chris Lasher <chris.lasher at gmail.com> wrote:
>
> 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.parrot". 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060616/e8a12d61/attachment.html>


More information about the Python-list mailing list