in reply to Re: Challenge: Transforming markups
in thread Challenge: Transforming markups

Argh

s/($named)|($http)|($camel)/$1?tansform_named($1):$2?transform_http($2):transform_camel($3)/gxe

Replies are listed 'Best First'.
Re^3: Challenge: Transforming markups
by LanX (Saint) on Dec 06, 2013 at 22:36 UTC
    OK, but the problem here are nested capture groups.

    you can't rely on $2 or $3 to be correct if $named is something like [[ ($http) ][ ($title) ]]

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      If you just don't like referring to the global %-, maybe something like:

      s/$regex/transform(%-)/ge sub transform { my %matches = @_; ... }

      Or you can do a second match.

      s/($regex)/transform($1)/ge sub transform { local $_ = shift; if (/^(\[\[($http)]\[($title)]])/) { return transform_named($2,$3) +} elsif (/^($http)/) { return transform_http($1) } ... }

      Or you might use something like this.

      while (1) { if (/\G($named)/gc) { print transform_named($1) } elsif (/\G($http)/gc) { print transform_http($1) } ... elsif (/\G(.)/gcs) { print $1 } else { last } }
        > Or you might use something like this.
        while (1) { if (/\G($named)/gc) { print transform_named($1) } elsif (/\G($http)/gc) { print transform_http($1) } ... elsif (/\G(.)/gcs) { print $1 } else { last } }

        Thanks I ended up using this approach, it's the most practical in my case! ¹

        Actually I knew and tried it before w/o luck, turned up that the /\G(.)/gcs was crucial.

        I remember \G to be tricky (or even buggy) in edge cases,

        (Don't really know why I need /s since there are no line breaks in single lines? ...have still to debug whats going on with pos here...)

        But it works now, thanks! =)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        ¹) The code in the OP might be a good way to design a parser generator but I dont need the complexity and the extra speed of one big regex.

      This can be easily fixed by using named backreferences. \k<named> would refer to a match of the pattern (?<named>...).