Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

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


in reply to Re^2: 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

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

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

    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:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2020-04-08 17:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (45 votes). Check out past polls.

    Notices?