Recursion regular expression (xtended)

S

sln

Original message replied to was posted to perl.beginners
8-14, but it didn't show up. EasyNews is acting weird.
To test, I'm posting it here...

-sln


Hi...

I want to replace in a javscript structure like the one below every
occurence of "{#...}", "{?...}", "{+...}" and "{=...}" through
something different (also nested):
function() {
test1 = "{#Caption}";
test2 = "{#Te{?st}}";
test3 = "{+date.{0}}";
}

with this regular expression
my( $open ) = qr/{[=+#?]/;
my( $close ) = qr/}/;
my( $not ) = qr/[^{}]++/;
do {
$found = $text =~ s/
(
$open
(?: $not | (?1) )
$close
)
/replace($1)/esxg;
} while ( $found );
I can handle case "test1" and "test2" but the case "test3" won't work.
Could some help me how I can change the expression that it match
"{+date.{0}}" without bothering about the "{0}" (this could also be
"{foo}").

I try it with:
my( $not ) = qr/(?>(?:(?!$open)|(?!$close))+)/;
but that won't work.

Thanks and best regards. And please excuse my english, I hope you will
understand everything, otherwise ask me ;-)

T.

I saw this by chance
You should post this complex question to the clpm group.

Something in here should be of help. Don't know what though.

-sln

-------------------------
use strict;
use warnings;

# Require {[=+#?]} but not {}

my $open = '\{';
my $ctrl = '[=+#?]';
my $close = '\}';

my $regex = qr/
( # GRP 1
$open
( ($ctrl) ( # GRP 2, 3, 4
(?:
( # GRP 5
$open
(?:
(?>(?:(?! $open$ctrl | $open | $close).)+)
| (?5)
)*
$close
)
|
(?>(?:(?! $open$ctrl | $open | $close).)+)
| (?1)
)*
) ) # end GRP 2, 4
$close
)
/xs;


my $code = join '',<DATA>;
print "\n",'-'x10,"\nBefore:\n",'-'x10,"\n$code\n",'-'x10,"\n";

my $found = 0;
while ($code =~ /$regex/g) {
print "found: $1\n";
$found = 1;
}
if ($found) {
1 while ($code =~ s/$regex/"['$3'$4]"/eg);
print "\n",'-'x10,"\nAfter:\n",'-'x10,"\n$code\n";
}

__DATA__
I want to replace in a javscript structure like the one below every
occurence of "{#...}", "{?...}", "{+...}" and "{=...}" through
something different (also nested):
function() {
test1 = "{#Caption}";
test2 = "{#Te{?st{8}}}";
test3 = "{+date.{0}}";
}
 
S

sln

Original message replied to was posted to perl.beginners
8-14, but it didn't show up. EasyNews is acting weird.
To test, I'm posting it here...
Oh its a mailing list that shows up as a usenet group.
I don't know how any of this is usefull or works so, not
wanting to get emails ... First and last time to the read-only
useless perl.beginners, er ah, newsgroup.
-sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top