Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Executing CGI/web form directives in regex substitution without pages of code

by jo37 (Scribe)
on Feb 19, 2020 at 21:46 UTC ( #11113186=note: print w/replies, xml ) Need Help??


in reply to Executing CGI/web form directives in regex substitution without pages of code

Something like this does work:

my $m = $regexI ? qr{(?i:$regexM)} : qr($regexM); my $s = $sv ? qr(^) : qr(); my $e = $ev ? qr($) : qr(); my $b = $ww ? qr(\b) : qr(); my $c = $ch ? qr([.,:;!?'"]) : qr(); foreach my $line (@data) { push @changed, $line =~ s{$s$c$b\K$m(?=$b$c$e)}{$regexR}r; }

HTH

-jo

  • Comment on Re: Executing CGI/web form directives in regex substitution without pages of code
  • Download Code

Replies are listed 'Best First'.
Re^2: Executing CGI/web form directives in regex substitution without pages of code
by Polyglot (Pilgrim) on Feb 19, 2020 at 22:14 UTC

    I appreciate your effort to be helpful. However, that code is giving me a syntax error, and I'm not sure of the reason. The error message in the log says:

    syntax error at LBE_FindReplace.pl line 567, near "s{$s$c$b\\K$m(?=$b$ +c$e)}{$regexR}r"

    Blessings,

    ~Polyglot~

      Where does the double backslash in \\K come from? Here is a complete working example:

      #!/usr/bin/perl use strict; use warnings; 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 = ''; my $m = $regexI ? qr{(?i:$regexM)} : qr($regexM); my $s = $sv ? qr(^) : qr(); my $e = $ev ? qr($) : qr(); my $b = $ww ? qr(\b) : qr(); my $c = $ch ? qr([.,:;!?'"]) : qr(); foreach my $line (@data) { push @changed, $line =~ s{$s$c$b\K$m(?=$b$c$e)}{$regexR}r; } return @changed; } foreach my $parm (qw(0:0:0:0:0 1:0:0:0:0 0:1:0:0:0 0:0:1:0:0 0:0:0:1:0 + 0:0:0:0:1)) { my @opts = split /:/, $parm; print "parm: $parm\n"; my @r = processReplacements('xxx', 'yyy', @opts, 'aaaxxxbbb', 'aaa xxx bbb', 'aaa:xxx:bbb', 'xxxbbb', 'aaa:XXX', 'a +aaxxx'); print "$_\n" foreach @r; print "\n"; }

      which gives:

      parm: 0:0:0:0:0 aaayyybbb aaa yyy bbb aaa:yyy:bbb yyybbb aaa:XXX aaayyy parm: 1:0:0:0:0 aaayyybbb aaa yyy bbb aaa:yyy:bbb yyybbb aaa:yyy aaayyy parm: 0:1:0:0:0 aaaxxxbbb aaa xxx bbb aaa:xxx:bbb yyybbb aaa:XXX aaaxxx parm: 0:0:1:0:0 aaaxxxbbb aaa xxx bbb aaa:xxx:bbb xxxbbb aaa:XXX aaayyy parm: 0:0:0:1:0 aaaxxxbbb aaa yyy bbb aaa:yyy:bbb xxxbbb aaa:XXX aaaxxx parm: 0:0:0:0:1 aaaxxxbbb aaa xxx bbb aaa:yyy:bbb xxxbbb aaa:XXX aaaxxx

      -jo

        That's a great question, and until you asked it, I hadn't even caught that. (I had just copy/pasted the error straight from the log, and noticed it was focused on that regex section.)

        The code did not contain a double backslash. I copied that line exactly as you had posted it, and it was and still is as follows in the code:

        foreach my $line (@data) { push @changed, $line =~ s{$s$c$b\K$m(?=$b$c$e)}{$regexR}r; }

        I haven't used the //r before. That is new to me. What does it do? I wondered if it might be the syntax error.

        Blessings,

        ~Polyglot~

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11113186]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2020-04-01 18:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (18 votes). Check out past polls.

    Notices?