hexcoder has asked for the wisdom of the Perl Monks concerning the following question:
I wonder if it is possible to do transformations on a string based on a named recursive capture.
For example given
how do you change all (?&expr) to something like Expression($+{expr})?my $pat = qr { (?(DEFINE) (?<number> (?: [0-9]+ )) (?<sign> (?: [-+] )) (?<expr> (?: (?&term) (?: \s* [-+] \s* (?&expr))? )) (?<term> (?: (?&factor) (?: \s* [/*] \s* (?&term))? )) (?<factor> (?: (?&number) | (?&sign) \s* (?&factor) | \( \s* (?&expr) \s* \))) ) (?&expr) }x;
A suitable string could be '1+2*3' and the expected result would be Expression(Expression(1)+Expression(Expression(2)*Expression(3))).
Also interesting: how to substitute a subexpression like (?&factor).
Thanks! hexcoder
Edit: fixed a typo in the replacement text
Edit2: Better wording in the second question.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: substitute with recursive regexps?
by Anonymous Monk on Sep 03, 2011 at 14:22 UTC | |
by hexcoder (Curate) on Sep 04, 2011 at 19:19 UTC | |
by Anonymous Monk on Sep 04, 2011 at 21:14 UTC |