[Python-ideas] Adding function checks to regex

Bruce Leban bruce at leapyear.org
Sat Mar 19 21:50:48 CET 2011

On Sat, Mar 19, 2011 at 4:33 AM, Peter Otten <__peter__ at web.de> wrote:

> Could the same be achieved without new regex syntax? I'm thinking of
> reusing
> named groups:
> re.findall(r"\b(?P<number>\d+)\b", text,
>           number=lambda s: 1 <= int(s) <= 10)

I like this alternative. (1) the function can simply operate on a string
rather than a regex object. (2) it makes the function optional, enabling
verification and testing of the regex to be separated from testing the
function. (3) it would make it easier to port code that uses this to other
languages and perhaps make it more likely to be adopted by other languages.

On Sat, Mar 19, 2011 at 9:35 AM, Calvin Spealman <ironfroggy at gmail.com>wrote:

> I am -1 on the whole idea.
> However, for the sake of argument, I'll say that if it was done I would not
> bind the callbacks at match time.
> Instead, they would be part of the compiled regex objects.
> r = re.compile(r"foo:(?C<check_bounds>\d+)", check_bounds=lambda d: 1 <=
> int(d) <= 100)
> and then r could be used like any other regex, and you don't need to know
> about the callbacks when actually using it, just to build it.

I'd want to understand likely use cases before deciding on early/late
binding of the callbacks. And I'm not sure the expressive power of this is
worth the effort.

--- Bruce
New Puzzazz newsletter: http://j.mp/puzzazz-news-2011-03
Make your web app more secure: http://j.mp/gruyere-security
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110319/e5c5e777/attachment.html>

More information about the Python-ideas mailing list