Detecting RegEx across Python versions

Lulu of the Lotus-Eaters mertz at gnosis.cx
Thu Feb 15 15:33:58 EST 2001


I have a module that wishes to match files by either "glob"
[fnmatch] or regex.  In a certain function, I hope to just check
a variable ('pattern') to see if it is a string or a compiled
regex, and match accordingly.  Unfortunately, Python 2 seems to
handle this matter differently than Python 1.5.2 (in a way that
does not seem necessary for the improved sre engine).

I have done the following, but would welcome something more
elegant

    [...]
    # kludge to detect a regular expression across python versions
    elif sys.version[0]=='1' and isinstance(pattern, re.RegexObject):
        if pattern.match(name):
            files.append(fname)
    elif sys.version[0]=='2' and type(pattern)==type(re.compile('')):
        if pattern.match(name):
            files.append(fname)
    elif type(pattern) is StringType:
        if fnmatch.fnmatch(name, pattern):
            files.append(fname)

Yours, Lulu...

--
---[ to our friends at TLAs (spread the word) ]--------------------------
Echelon North Korea Nazi cracking spy smuggle Columbia fissionable Stego
White Water strategic Clinton Delta Force militia TEMPEST Libya Mossad
---[ Postmodern Enterprises <mertz at gnosis.cx> ]--------------------------





More information about the Python-list mailing list