escape sequences and string comparisons

Fredrik Lundh fredrik at effbot.org
Wed Jan 10 12:25:48 EST 2001


jkndeja at my-deja.com wrote:
> OK, perhaps I shouldn't have written 'RE' match above. What I was
> hoping/expecting is that this:
>
> >>> import re
> >>> s = r'[123]'
> >>> r = re.escape(s)
> >>> r
> '\\[123\\]'
> >>> r == s
> 0
> >>>
>
> or something similar, would give 1, not zero

try:

    for s in all_possible_string_values:

        r = s
        assert r == s

        assert eval(repr(s)) == s

        m = re.match(re.escape(s), s)
        assert m != None and m.span() == (0, len(s))

except AssertionError:
    print "this message will never be printed!"

Cheers /F





More information about the Python-list mailing list