in reply to Re: Passing a regex from a CGI HTML form
in thread Passing a regex from a CGI HTML form
There are safer way to use eval, or even avoid it all together
Re^3: My Favourite Regex Tools (Was: Parsing a Variable Format String)
It can be as simple as
use String::Interpolate::RE qw( strinterp ); print Substitution( "input string", "pattern", "replacement", "flags" ); sub Substitution { my( $in, $re, $rep, $flags ) = @_; my $global = $flags =~ m{g}i; my $qrFlags = join '', $flags =~ m{[msixpodualn]}i; $qrFlags = "(?$qrFlags)"; $re = qr{$qrFlags$re}; if( $global ){ $in =~ s{$re}{ Replace($rep, \%+,{1=>$1,2=>$2,3=>$3}); }gex; } else { $in =~ s{$re}{ my $vars = { %+, 1=>$1, 2=>$2, 3=>$3, }; strinterp( $rep, $vars ); }ex; } } sub Replace { my( $rep, $named, $numed ) = @_; my $vars = { %$named, %$numed, 'bananas','bananas' ); return strinterp( $rep, $vars ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Passing a regex from a CGI HTML form (user supplied regex substitution without eval)
by trippledubs (Deacon) on Sep 01, 2016 at 01:34 UTC | |
by Anonymous Monk on Sep 01, 2016 at 02:21 UTC | |
by trippledubs (Deacon) on Sep 01, 2016 at 04:37 UTC | |
by Anonymous Monk on Sep 01, 2016 at 06:51 UTC | |
by trippledubs (Deacon) on Sep 01, 2016 at 12:04 UTC | |
| |
|
Re^3: Passing a regex from a CGI HTML form (user supplied regex substitution without eval)
by Anonymous Monk on Aug 31, 2016 at 21:51 UTC |