in reply to Re: Replacement Regex
in thread Replacement Regex
This one will handle nested parens:
#!/usr/bin/perl -w use strict; $_ = '$a ($a (hi) $a) $a (()$a)'; my $re; $re = qr/(?:[^\(\)]*|\((??{$re})\))+/; s/(\($re\))|\$a/$1?$1:'$b'/eg; print;
Update: should've referred to perlre manpage instead of relying on memory, the version there makes it somewhat nicer:
my $re; $re = qr/\((?:(?>[^()]*)|(??{$re}))*\)/; s/($re)|\$a/$1?$1:'$b'/eg;
Update: It appears to broken in 5.6.0 (gives output as tachyon shows below), but it works for me with 5.6.1.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Replacement Regex
by tachyon (Chancellor) on Mar 20, 2002 at 19:29 UTC | |
by I0 (Priest) on Mar 22, 2002 at 00:34 UTC |