When not to use an RE -- an example

Ian McMeans imcmeans at telus.net
Sun Apr 20 03:15:56 CEST 2003


How about using a dictionary? I wish we had dictionary comprehensions
:(

>>> user_input = " ZZZZZZZ\n\n"
>>> d = dict( [(x, None) for x in list(user_input.strip())] )
>>> len(d.keys())
1

I don't really understand your regular expression...
"^(?:(.)(?=\1))+\1\Z"
Why the lookahead assertion following the first capture group,
followed by the capture group outside the parantheses? It seems crazy
to me.

".{2,}" is repeated characters, ".{2,}\s*$" is repeated characters
followed by optional whitespace, with nothing following it.

sjmachin at lexicon.net (John Machin) wrote in message news:<c76ff6fc.0304191459.2adf73a at posting.google.com>...
> I needed a check for strings consisting of repeated characters -- like
> when users type "ZZZZZZZ" instead of "UNKNOWN" into a database field.
> After implementing the obvious overlapping-substring comparison, I got
> to thinking how this could be done with REs. The following resulted:
> 
> import re
> repeats1 = re.compile(r"^(?:(.)(?=\1))+\1\Z", re.DOTALL).match
> def repeats2(s):
>    return len(s) > 1 and s[1:] == s[:-1]
> for testvalue, expected in zip(
>    ['','x','xx','xxx','xxxxxx','xy','xxy','xyy','\n\n\n','aaa\n'],
>    [0,  0,  1,   1,    1,       0,   0,    0,    1,       0     ]):
>    print repr(testvalue), not not repeats1(testvalue),
> repeats2(testvalue), expected
> 
> Pergly/phugly, eh? Note the effort required to ensure the newline
> cases worked.




More information about the Python-list mailing list