Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Challenge: Transforming markups

by LanX (Saint)
on Dec 06, 2013 at 22:36 UTC ( [id://1066070]=note: print w/replies, xml ) Need Help??


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

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)

Replies are listed 'Best First'.
Re^4: Challenge: Transforming markups
by Anonymous Monk on Dec 06, 2013 at 23:27 UTC

    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.

Re^4: Challenge: Transforming markups
by hdb (Monsignor) on Dec 07, 2013 at 12:26 UTC

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1066070]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found