Hello Monks,

Movable Type has a "regex_replace" tag modifier that takes two arguments, a regular expression, and a replacement expression, and returns the modified string.

My problem is that I'd like to write a plugin that returns an array of the individual modified strings instead of the whole string. I thought that would be easy. And it may be, but not for me. The challenge is that while the match regex operator can return an array, the same is not true for substitutions. So while the following returns matches, the behaviour is not the same for substitutions.

@matches = $text =~ /$regex/smg)

Here's the original code for MT's "regex_replace" modifier. I'd offer my attempts, but really I tried a lot of things and just got nowhere. I end up just getting the number of substitutions whereas I'd like to capture the substitutions themselves.

TIA!

=head2 regex_replace Applies a regular expression operation on the input. This filter accep +ts B<two> input values: one is the pattern, the second is the replacement +. B<Example:> <$mt:EntryTitle regex_replace="/\s*\[.+?\]\s*$/",""$> This would strip any bracketed phrase from the end of the entry title field. =cut sub _fltr_regex_replace { my ( $str, $val, $ctx ) = @_; # This one requires an array return $str unless ref($val) eq 'ARRAY'; my $patt = $val->[0]; my $replace = $val->[1]; if ( $patt =~ m!^(/)(.+)\1([A-Za-z]+)?$! ) { $patt = $2; my $global; if ( my $opt = $3 ) { $global = 1 if $opt =~ m/g/; $opt =~ s/[ge]+//g; $patt = "(?$opt)" . $patt; } my $re = eval {qr/$patt/}; if ( defined $re ) { $replace =~ s!\\\\(\d+)!\$1!g; # for php, \\1 is how you +write $1 $replace =~ s!/!\\/!g; eval '$str =~ s/$re/' . $replace . '/' . ( $global ? 'g' : + '' ); if ($@) { return $ctx->error("Invalid regular expression: $@"); } } } return $str; }
(update: corrected title spelling)

In reply to capture global substitution in an array by wrinkles

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.