Regular Expression Question

Tim Peters tim.one at home.com
Tue Apr 3 20:42:26 EDT 2001


[Wesley Witt]
> This is probably a simple question, but I can't seem to find the answer
> anywhere.
>
> I want a regular expression that will match ALL lines that do NOT
> contain the string "skip".
> ...

Like everyone else here, I recommend using the string find() method instead,
for this specific case.

More generally, if you *need* a regexp, there's a trivial solution:  don't
screw with the regexp, invert the result of the *test*:

    searcher = re.compile("skip").search
    while 1:
        ...
        if searcher(line):
            # found it
        else:
            # no "skip" in this line!

A funkier way is to fool around with negative lookahead assertions, like

    searcher = re.compile("(?!.*skip)", re.DOTALL).match

This fails to match if it can find "skip", and matches an empty string at the
start if it can't find "skip".

pick-your-poison-but-look-in-the-mirror-as-you're-dying<wink>-ly y'rs
    - tim





More information about the Python-list mailing list