variables is regexp

Richie Hindle richie at entrian.com
Thu Jun 17 08:58:04 EDT 2004


[Repost; apologies to anyone who sees this twice]

[Manlio]
> It would be nice to have the possibility to define variables inside
> regexp.
> 
> Ad example:
> (?$identifier=regexp) for defining variables
> (?$identifier) for accessing variables.

You can, using (?P<name>regex) to define them and (?P=name) to access them.
Here's my favourite example of regular expression abuse, using them to find
prime numbers:

>>> import re
>>> for i in range(2, 1000):
>>>     if not re.match(r'(?P<factor>xx+)(?P=factor)+$', 'x'*i):
>>>         print i
2
3
5
7
11
...

-- 
Richie Hindle
richie at entrian.com





More information about the Python-list mailing list