Regular expression question
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.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regular expression question
On 8/13/10 Fri Aug 13, 2010 1:47 PM, "irata"
<tobias.wagener [at] googlemail.com> scribbled:
> Hi...
>
> I want to replace in a javscript structure like the one below every
> occurence of "{#...}", "{?...}", "{+...}" and "{=...}" through
> something different (also nested):
Check out the Text::Balanced module, available at CPAN:
<http://search.cpan.org/~adamk/Text-Balanced-2.02/lib/Text/Balanced.pm>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regular expression question
On Aug 13, 1:47=A0pm, tobias.wage... [at] googlemail.com (irata) wrote:
>
> I want to replace in a javscript structure like the one below every
> occurence of "{#...}", "{?...}", "{+...}" and "{=3D...}" through
> something different (also nested):
> =A0 =A0function() {
> =A0 =A0 =A0 test1 =3D "{#Caption}";
> =A0 =A0 =A0 test2 =3D "{#Te{?st}}";
> =A0 =A0 =A0 test3 =3D "{+date.{0}}";
> =A0 =A0}
>
> with this regular expression
> =A0 my( $open ) =A0 =3D qr/{[=3D+#?]/;
> =A0 my( $close ) =A0=3D qr/}/;
> =A0 my( $not ) =A0 =A0=3D qr/[^{}]++/;
> =A0 do {
> =A0 =A0 =A0$found =3D $text =3D~ s/
> =A0 =A0 =A0 (
> =A0 =A0 =A0 =A0 $open
> =A0 =A0 =A0 =A0 (?: $not | (?1) )
> =A0 =A0 =A0 =A0 $close
> =A0 =A0 =A0 )
> =A0 =A0 =A0/replace($1)/esxg;
> =A0 } 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:
> =A0 my( $not ) =3D qr/(?>(?:(?!$open)|(?!$close))+)/;
> but that won't work.
>
[Your question would probably be better posted in
comp.lang.perl.misc]
One good debugging technique to track what the regex
engine is doing: use re 'debug'
I believe the problem is that once the inner {0} is found
the recursion pattern fails because $open is {[=3D+#?] and
{0} won't match.
You could perhaps make it work by checking whether
you're recursing to alter the pattern accordingly:
untested tweak:
my( $open ) =3D qr/ (? (R) [=3D+#?] | [=3D+#?]? ) /x;
clear as mud?
--
Charles DeRykus
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regular expression question
On Aug 14, 6:28=A0am, dery... [at] gmail.com ("C.DeRykus") wrote:
> On Aug 13, 1:47=A0pm, tobias.wage... [at] googlemail.com (irata) wrote:
>
>
>
>
>
> > I want to replace in a javscript structure like the one below every
> > occurence of "{#...}", "{?...}", "{+...}" and "{=3D...}" through
> > something different (also nested):
> > =A0 =A0function() {
> > =A0 =A0 =A0 test1 =3D "{#Caption}";
> > =A0 =A0 =A0 test2 =3D "{#Te{?st}}";
> > =A0 =A0 =A0 test3 =3D "{+date.{0}}";
> > =A0 =A0}
>
> > with this regular expression
> > =A0 my( $open ) =A0 =3D qr/{[=3D+#?]/;
> > =A0 my( $close ) =A0=3D qr/}/;
> > =A0 my( $not ) =A0 =A0=3D qr/[^{}]++/;
> > =A0 do {
> > =A0 =A0 =A0$found =3D $text =3D~ s/
> > =A0 =A0 =A0 (
> > =A0 =A0 =A0 =A0 $open
> > =A0 =A0 =A0 =A0 (?: $not | (?1) )
> > =A0 =A0 =A0 =A0 $close
> > =A0 =A0 =A0 )
> > =A0 =A0 =A0/replace($1)/esxg;
> > =A0 } 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:
> > =A0 my( $not ) =3D qr/(?>(?:(?!$open)|(?!$close))+)/;
> > but that won't work.
>
> [Your question would probably be better posted in
> =A0 =A0 comp.lang.perl.misc]
>
> One good debugging technique to track what the regex
> engine is doing: =A0 use re 'debug'
>
> I believe the problem is that once the inner {0} is found
> the recursion pattern fails because $open is =A0{[=3D+#?] and
> {0} won't match.
>
> You could perhaps make it work by checking whether
> you're recursing to alter the pattern accordingly:
>
> untested tweak:
>
> my( $open ) =A0 =3D qr/ (? (R) [=3D+#?] | [=3D+#?]? ) /x;
>
Good thing I wrote "untested".
Make that tweak:
my( $open ) =3D qr/{(? (R) [=3D+#?] | [=3D+#?]?)/;
--
Charles DeRykus
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/