I have a database being edited via a web form which needs to allow user-specified substitutions. By providing checkboxes in the form for things like "Match at start of line", "Match at end of line", "Case insensitive", etc., along with the values for the actual text to search for and to replace with, I can provide PERL with a regular expression (regex/regexp) substitution to run against the text in the database. However, I have been unable to use it, and was forced to create pages of code to accommodate all of the possible options, as is shown further below.
What I would like is something more like this:
$substitute = qq|s~$sv$ch$ww$searchQuery$ww$ch$ev~$replacement~g$in;|; $line =~ $substitute;
. . . Where $sv would be a "^" if the user had requested the match to start at the beginning of the line, or be blank ("") otherwise, $ev would optionally be the "$" for end of line matching, etc. When I tried this approach, however, it failed. So I ended up doing the following instead:
sub processReplacements { my $regexM = shift @_; #TERM(S) TO MATCH my $regexR = shift @_; #REPLACEMENT TERM my $regexI = shift @_; #FLAG FOR CASE-INSENSITVE SUBSTITUTION my $sv = shift @_; # $sv => START, E.G. /^(.*)/; my $ev = shift @_; # $ev => END, E.G. /(.*)$/; my $ww = shift @_; # $ww => WHOLE-WORD, E.G. /\b(.*)\b/; my $ch = shift @_; # $ch => DELIMIT CHARS, # E.G. /[.,:;!?'"](.*)[.,:;!?'"]/; my @data = @_; # INCOMING ARRAY my @changed = (); # OUTGOING ARRAY my $line = ''; my $linehead = ''; my $sourceline = ''; $regexM = decode("utf8", $regexM); $regexR = decode("utf8", $regexR); foreach $line (@data) { chomp $line; $line =~ s/\s+$//; $line =~ s/((?:\d+\t)+)//; $linehead = $1; #KEEP A COPY OF ORIGINAL FOR LATER COMPARISON $sourceline = $line; if ($regexI) { #CASE INSENSITIVE if ($sv) { #START VERSE if ($ev) { #END VERSE if ($ww) { #WHOLE WORD if ($ch) { #CHAR DELIMITERS $line =~ s~^[$ch]$regexM[$ch]\b$~$regexR~gie; } else { #NO CHAR DELIMITS $line =~ s~^$regexM\b$~$regexR~gie; } } else { #NOT WHOLE if ($ch) { #CHAR DELIMITERS $line =~ s~^[$ch]$regexM[$ch]$~$regexR~gie; } else { #NO CHAR DELIMITS $line =~ s~^$regexM$~$regexR~gie; } } } else { #NOT END VERSE if ($ww) { #WHOLE WORD if ($ch) { #CHAR DELIMITERS $line =~ s~^\b[$ch]$regexM[$ch]\b~$regexR~gie; } else { #NO CHAR DELIMITS $line =~ s~^$regexM\b~$regexR~gie; } } else { #NOT WHOLE if ($ch) { #CHAR DELIMITERS $line =~ s~^[$ch]$regexM[$ch]~$regexR~gie; } else { #NO CHAR DELIMITS $line =~ s~^$regexM~$regexR~gie; } } } } else { #NOT START if ($ev) { #END VERSE if ($ww) { #WHOLE WORD if ($ch) { #CHAR DELIMITERS $line =~ s~\b[$ch]$regexM[$ch]\b$~$regexR~gie; } else { #NO CHAR DELIMITS $line =~ s~\b$regexM\b$~$regexR~gie; } } else { #NOT WHOLE if ($ch) { #CHAR DELIMITERS $line =~ s~[$ch]$regexM[$ch]$~$regexR~gie; } else { #NO CHAR DELIMITS $line =~ s~$regexM$~$regexR~gie; } } } else { #NOT END VERSE if ($ww) { #WHOLE WORD if ($ch) { #CHAR DELIMITERS $line =~ s~\b[$ch]$regexM[$ch]\b~$regexR~gie; } else { #NO CHAR DELIMITS $line =~ s~\b$regexM\b~$regexR~gie; } } else { #NOT WHOLE if ($ch) { #CHAR DELIMITERS $line =~ s~[$ch]$regexM[$ch]~$regexR~gie; } else { #NO CHAR DELIMITS $line =~ s~$regexM~$regexR~gie; } } } } } else { #CASE SENSITIVE if ($sv) { #START VERSE if ($ev) { #END VERSE if ($ww) { #WHOLE WORD if ($ch) { #CHAR DELIMITERS $line =~ s~^\b[$ch]$regexM[$ch]\b$~$regexR~ge; } else { #NO CHAR DELIMITS $line =~ s~^\b$regexM\b$~$regexR~ge; } } else { #NOT WHOLE if ($ch) { #CHAR DELIMITERS $line =~ s~^[$ch]$regexM[$ch]$~$regexR~ge; } else { #NO CHAR DELIMITS $line =~ s~^$regexM$~$regexR~ge; } } } else { #NOT END if ($ww) { #WHOLE WORD if ($ch) { #CHAR DELIMITERS $line =~ s~^\b[$ch]$regexM[$ch]\b~$regexR~ge; } else { #NO CHAR DELIMITS $line =~ s~^\b$regexM\b~$regexR~ge; } } else { #NOT WHOLE if ($ch) { #CHAR DELIMITERS $line =~ s~^[$ch]$regexM[$ch]~$regexR~ge; } else { #NO CHAR DELIMITS $line =~ s~^$regexM~$regexR~ge; } } } } else { #NOT START if ($ev) { #END VERSE if ($ww) { #WHOLE WORD if ($ch) { #CHAR DELIMITERS $line =~ s~\b[$ch]$regexM[$ch]\b$~$regexR~ge; } else { #NO CHAR DELIMITS $line =~ s~\b$regexM\b$~$regexR~ge; } } else { #NOT WHOLE if ($ch) { #CHAR DELIMITERS $line =~ s~[$ch]$regexM[$ch]$~$regexR~ge; } else { #NO CHAR DELIMITS $line =~ s~$regexM$~$regexR~ge; } } } else { #NOT END if ($ww) { #WHOLE WORD if ($ch) { #CHAR DELIMITERS $line =~ s~\b[$ch]$regexM[$ch]\b~$regexR~ge; } else { #NO CHAR DELIMITS $line =~ s~\b$regexM\b~$regexR~ge; } } else { #NOT WHOLE if ($ch) { #CHAR DELIMITERS $line =~ s~[$ch]$regexM[$ch]~$regexR~ge; } else { #NO CHAR DELIMITS $line =~ s~$regexM~$regexR~ge; } } } } } if ($line ne $sourceline) { push @changed, "$linehead$sourceline\t$line\n"; }; $line = $linehead.$line; } return @changed; } # END SUB processReplacements
To me, that doesn't seem very Perlish. It is certainly far more unwieldy to work with. Can this whole subroutine be replaced with just a few lines as per my first attempt?
Blessings,
~Polyglot~
In reply to Executing CGI/web form directives in regex substitution without pages of code by Polyglot
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |