regex matching question

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat May 19 13:57:02 EDT 2007


In <1179595319.239229.262160 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