=head2 regex_replace Applies a regular expression operation on the input. This filter accepts B input values: one is the pattern, the second is the replacement. B <$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; }