Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: capture global substituion inot an array

by wrinkles (Pilgrim)
on Apr 30, 2012 at 17:52 UTC ( #968117=note: print w/ replies, xml ) Need Help??


in reply to Re: capture global substituion inot an array
in thread capture global substitution in an array

Moritz thanks again for your insight, it helped me greatly. I pushed the RegexList Movable Type plugin to github.

The plugin is a tag modifer, meaning you place it into any Movable Type tag that outputs text. The modifier has three arguments. One specifies the substrings to process, and the other two are the "search" regex expression and the replace expression. The result of course is an array of processed substrings which are passed directly within the parent tag to a Movable type array variable (via the built-in setvar modifier). The optional fourth argument to the regex_list modifier defines an alternate capture variable (1-9) to the first match expression (the default is the whole match ($&). I've pasted the main module code below.

package RegexList::Plugin; use strict; use MT 4; sub regex_list { my ( $str, $val, $ctx ) = @_; my $app = MT->app; my @matches; my @subs; # Requires an array die "regex_list requires multiple arguments." unless ref($val) eq 'A +RRAY'; my $subst_patt = $val->[0]; my $replace = $val->[1]; my $match_patt = $val->[2]; my $capture = $val->[3]; # Restrict $capture to single-digit or default is & $capture = $capture =~ /^\s*(\d)\s*/; $capture = ($capture || '&'); #store match from first regex into an array if ( $match_patt =~ m!^(/)(.+)\1([A-Za-z]+)?$! ) { $match_patt = $2; if ( my $opt = $3 ) { $opt =~ s/[ge]+//g; # Mode modifier is moved into the regex using ($modifier) syntax $match_patt = "(?$opt)" . $match_patt; } my $re = eval {qr/$match_patt/}; if ( defined $re ) { eval { while ($str =~ /$re/go) { no strict 'refs'; push (@matches, $$capture); use strict 'refs'; $app->log({ message => "Pushed '" . $& . "' to matches." }); } }; if ($@) { return $ctx->error("Invalid regular expression: $@"); } } } if ( $subst_patt =~ m!^(/)(.+)\1([A-Za-z]+)?$! ) { $subst_patt = $2; my $global; if ( my $opt = $3 ) { $global = 1 if $opt =~ m/g/; $opt =~ s/[ge]+//g; # Mode modifier is moved into the regex using ($modifier) syntax $subst_patt = "(?$opt)" . $subst_patt; } my $re = eval {qr/$subst_patt/}; if ( defined $re ) { $replace =~ s!\\\\(\d+)!\$1!g; # for php, \\1 is how you write +$1 $replace =~ s!/!\\/!g; for my $m (@matches) { eval '$m =~ s/$re/' . $replace . '/o' . ( $global ? 'g' : '' ) +; push (@subs, $m); $app->log({ message => "Pushed '" . $m . "' to subs." }); if ($@) { return $ctx->error("Invalid regular expression: $@"); } } } } return \@subs; } sub mt_log { my ($app, $msg) = @_; $app->log({ message => $msg }); } 1;


Comment on Re^2: capture global substituion inot an array
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2014-12-26 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (168 votes), past polls