[Python-ideas] Adding function checks to regex

MRAB python at mrabarnett.plus.com
Mon Mar 28 21:34:15 CEST 2011

On 28/03/2011 18:46, Daniel da Silva wrote:
>     I would approach that with
>     numbers = (int(m.group()) for m in re.finditer(r"\b\d+\b"))
>     numbers = [n for n in numbers if 1 <= n <= 10]
> To follow up on this: he has pointed out an existing way of doing
> something that fully covers the goal of your addition. The current way
> is both straightforward, elegant, and self-describes what it is doing, I
> believe. I think if we have an obvious way to do it, we usually want to
> be consistent with our normal attempt of having one obvious way to do it.
> If his way wasn't obvious, you may not be Dutch.
I was thinking about 2 possible uses:

1. Where you would have a regex in a configuration or setup file, or
validation for a field, but with extra checks which are tricky or
impossible in a regex, eg date ranges.

2. Where you want to perform a check during the matching, much in the
way that you would use a lookahead or lookbehind.

So far no-one has been able to come up with a convincing real world use
case. Still, it's better to make a bad suggestion than not to make a
good one. :-)

More information about the Python-ideas mailing list