[Python-Dev] Adding function checks to regex

Guido van Rossum guido at python.org
Sat Mar 19 01:38:33 CET 2011


Probably best discussed on python-ideas...
On Fri, Mar 18, 2011 at 4:04 PM, MRAB <python at mrabarnett.plus.com> wrote:
> Some of those who are relative new to regexes sometimes ask how to write a
> regex which checks that a number is in a range or is a valid date. Although
> this may be possible, it certainly isn't easy.
>> From what I've read, Perl has a way of including code in a regex, but I
> don't think that's a good idea
>> However, it occurs to me that there may be a case for being able to call a
> supplied function to perform such checking.
>> Borrowing some syntax from Perl, it could look like this:
>>    def range_check(m):
>        return 1 <= int(m.group()) <= 10
>>    numbers = regex.findall(r"\b\d+\b(*CALL)", text, call=range_check)
>> The regex module would match as normal until the "(*CALL)", at which point
> it would call the function. If the function returns True, the matching
> continues (and succeeds); if the function returns False, the matching
> backtracks (and fails).
>> The function would be passed a match object.
>> An extension, again borrowing the syntax from Perl, could include a tag like
> this:
>>    numbers = regex.findall(r"\b\d+\b(*CALL:RANGE)", text, call=range_check)
>> The tag would be passed to the function so that it could support multiple
> checks.
>> Alternatively, a tag could always be passed; if no tag is provided then None
> would be passed instead.
>> There's also the additional possibility of providing a dict of functions
> instead and using the tag to select the function which should be called.
>> I'd be interested in your opinions.
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/guido%40python.org
>
-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /