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

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.