On Aug 13, 1:47 pm, tobias.wage...@googlemail.com (irata) wrote:

>
> 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.
>

[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  {[=+#?] 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 )   = qr/ (? (R) [=+#?] | [=+#?]? ) /x;


clear as mud?

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to