regex recursive matching (regex 2015.07.19)
Ben Bacarisse
ben.usenet at bsb.me.uk
Wed Aug 19 10:41:06 EDT 2015
MRAB <python at mrabarnett.plus.com> writes:
> On 2015-08-18 22:55, Ben Bacarisse wrote:
>> Neal Becker <ndbecker2 at gmail.com> writes:
>>
>>> Trying regex 2015.07.19
>>>
>>> I'd like to match recursive parenthesized expressions, with groups such that
>>> '(a(b)c)'
>>>
>>> would give
>>> group(0) -> '(a(b)c)'
>>> group(1) -> '(b)'
>>>
>>> but that's not what I get
>>>
>>> import regex
>>>
>>> #r = r'\((?>[^()]|(?R))*\)'
>>> r = r'\(([^()]|(?R))*\)'
>>> #r = r'\((?:[^()]|(?R))*\)'
>>> m = regex.match (r, '(a(b)c)')
>>
>> The (?R) syntax is Perl -- it's no implemented in Python. Python and
>> Perl regexs are very similar in syntax (for very good reasons) but
>> neither (?R) nor the numbered or named versions of it are in Python.
>>
> He's using the regex module from PyPI:
Ah, right. Then he might find
r = r'(\((?:[^()]|(?R))*\))'
more suitable when combined with captures() rather than groups():
regex.match(r, '(a(b)c)').captures(1)
['(b)', '(a(b)c)']
--
Ben.
More information about the Python-list
mailing list