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

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~

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

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

    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~

        It means:

        Return substitution and leave the original string untouched.

        Maybe it is not available in your version. But you may work around this.

        -jo

        The  /r modifier for  s/// substitution was added with Perl version 5.14 (see Regexp Quote-Like Operators in perlop). What version of Perl are you using?


        Give a man a fish:  <%-{-{-{-<