The Opposite of re match

Ben S newsgroups2002 at
Thu Aug 22 07:31:44 CEST 2002

Matt Gerrans wrote:
> I have a script which tests user input on a collection of regular
> expressions; if it fails all of them the input is rejected, but I'd
> like to be able to display a useful message like "You entered
> 'abc_1q23.ext' which is pretty close to the 'aaa_nnn.ext' format, but
> you cannot have a letter in the number field."    Since the re test
> will simply return None for all the available format choices, there
> is no object to work with, of course. Trying to anticipate every
> possible zany form of input that may arrive could be a pretty big
> task, too.

I don't know the exact answer to your question. But are there no general
patterns to the formats?  You could perhaps run some basic checks that
help to narrow it down, for example to see if just the extension matches
or the length matches, or match certain parts in isolation. That way,
you know the general format is ok but the specifics are not. The format
for the above example is quite clearly in 4 parts - 3 letters, an
underscore, 3 numbers, and an extension, so you could get an accurate
answer with just 4 patterns, I think. Maybe not the answer you wanted,
but better than nothing...

Ben Sizer

More information about the Python-list mailing list