regex recursive matching (regex 2015.07.19)
python at mrabarnett.plus.com
Tue Aug 18 18:36:18 CEST 2015
On 2015-08-18 15:25, Neal Becker wrote:
> Trying regex 2015.07.19
> I'd like to match recursive parenthesized expressions, with groups such that
> 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)')
> Out: ('c',)
You can't capture them into different groups in the general case; it
won't create capture groups dynamically.
Capture into 1 group and then use the .captures method:
r = r'(\([^()]*(?:(?R)[^()]*)*\))'
m = regex.match(r, '(a(b)c)')
More information about the Python-list