regex matching question
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Sat May 19 19:23:56 EDT 2007
En Sat, 19 May 2007 19:40:39 -0300, bullockbefriending bard
<kinch1967 at gmail.com> escribió:
> from my cursory skimming of friedl, i get the feeling that the all
> pairs different constraint would give rise to some kind of fairly
> baroque expression, perhaps likely to bring to mind the following
> quotation from samuel johnson:
>
> "Sir, a woman's preaching is like a dog's walking on his hind legs.
> It is not done well; but you are surprised to find it done at all."
Try this, it's not as hard, just using match and split (with the regular
expression propossed by MR):
import re
regex = re.compile(r'(\d+,\d+/){5}\d+,\d+')
def checkline(line):
if not regex.match(line):
raise ValueError("Invalid format: "+line)
for pair in line.split("/"):
a, b = pair.split(",")
if a==b:
raise ValueError("Duplicate number: "+line)
Here "all pairs different" means "for each pair, both numbers must be
different", but they may appear in another pair. That is, won't flag
"1,2/3,4/3,5/2,6/8,3/1,2" as invalid, but this wasn't clear from your
original post.
--
Gabriel Genellina
More information about the Python-list
mailing list