regex matching question

bullockbefriending bard kinch1967 at gmail.com
Sat May 19 18:40:39 EDT 2007


thanks for your suggestion. i had already implemented the all pairs
different constraint in python code. even though i don't really need
to give very explicit error messages about what might be wrong with my
data (obviously easier to do if do all constraint validation in code
rather than one regex), there is something to be said for your
suggestion to simplify my regex further - it might be sensible from a
maintainability/readability perspective to use regex for  *format*
validation and then validate all *values* in code.

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."

however, being human, sometimes some things should be done, just
because they can :)... so if anyone knows hows to do it, i'm still
interested, even if just out of idle curiosity!

On May 20, 12:57 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> In <1179595319.239229.262... at l77g2000hsb.googlegroups.com>,
>
>
>
> bullockbefriending bard wrote:
> > first, regex part:
>
> > I am new to regexes and have come up with the following expression:
> >      ((1[0-4]|[1-9]),(1[0-4]|[1-9])/){5}(1[0-4]|[1-9]),(1[0-4]|[1-9])
>
> > to exactly match strings which look like this:
>
> >      1,2/3,4/5,6/7,8/9,10/11,12
>
> > i.e. 6 comma-delimited pairs of integer numbers separated by the
> > backslash character + constraint that numbers must be in range 1-14.
>
> > i should add that i am only interested in finding exact matches (doing
> > some kind of command line validation).
>
> > [...]
>
> > the idea in the above code being that i want to use the regex match as
> > a test of whether or not the input string (results) is correctly
> > formatted. if the string results is not exactly matched by the regex,
> > i want my program to barf an exception and bail out. apart from
> > whether or not the regex is good idiom, is my approach suitably
> > pythonic?
>
> I would use a simple regular expression to extract "candidates" and a
> Python function to split the candidate and check for the extra
> constraints.  Especially the "all pairs different" constraint is something
> I would not even attempt to put in a regex.  For searching candidates this
> should be good enough::
>
>   r'(\d+,\d+/){5}\d+,\d+'
>
> Ciao,
>         Marc 'BlackJack' Rintsch





More information about the Python-list mailing list