Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Matching backslash in regexp negative lookbehind

by AnomalousMonk (Chancellor)
on Jan 02, 2018 at 20:10 UTC ( #1206558=note: print w/replies, xml ) Need Help??


in reply to Matching backslash in regexp negative lookbehind

Wouldn't it be better (or at least easier on the noggin) to use a two-pass approach? (Uses Perl 5.10+  \K regex extension.)

use 5.010; use warnings; use strict; use Test::More 'no_plan'; use Test::NoWarnings; use constant SENSITIVE => qr{ password | user }xms;; VECTOR: for my $ar_vector ( [ q/{'password' => 'secret'}/, q/{'password' => '***'}/, ], [ q/{'password' => 'sec\'ret'}/, q/{'password' => '***'}/, ], [ q/{"password" => "secret"}/, q/{"password" => "***"}/, ], [ q/{"password" => "sec\"ret"}/, q/{"password" => "***"}/, ], [ q/{'password' : 'secret'}/, q/{'password' : '***'}/, ], [ q/{'password' : 'sec\'ret'}/, q/{'password' : '***'}/, ], [ q/{"password" : "secret"}/, q/{"password" : "***"}/, ], [ q/{"password" : "sec\"ret"}/, q/{"password" : "***"}/, ], [ q/{'user' => 'secret'}/, q/{'user' => '***'}/, ], [ q/{'user' => 'sec\'ret'}/, q/{'user' => '***'}/, ], [ q/{"user" => "secret"}/, q/{"user" => "***"}/, ], [ q/{"user" => "sec\"ret"}/, q/{"user" => "***"}/, ], [ q/{'user' : 'secret'}/, q/{'user' : '***'}/, ], [ q/{'user' : 'sec\'ret'}/, q/{'user' : '***'}/, ], [ q/{"user" : "secret"}/, q/{"user" : "***"}/, ], [ q/{"user" : "sec\"ret"}/, q/{"user" : "***"}/, ], ) { my ($string, $expected) = @$ar_vector; is elide(SENSITIVE, $string), $expected, "'$string' -> '$expected'"; } # end for VECTOR done_testing; # subroutines ###################################################### use constant { DQ => qr{ " [^\\"]* (?: \\. [^\\"]*)* " }xms, DQ_MASK => q{"***"}, SQ => qr{ ' [^\\']* (?: \\. [^\\']*)* ' }xms, SQ_MASK => q{'***'}, CONNECTOR => qr{ => | : }xms, }; sub elide { my ($key, $string, ) = @_; $string =~ s{ " $key " \s* ${ \CONNECTOR } \s* \K ${ \DQ } } {${ \DQ_MASK }}xmsg; $string =~ s{ ' $key ' \s* ${ \CONNECTOR } \s* \K ${ \SQ } } {${ \SQ_MASK }}xmsg; return $string; }
(Update: Using 5.10 possessive quantifiers or older, equivalent atomic groupings might make the substitutions slightly faster, but probably only for very long sub-strings.)
Output:
c:\@Work\Perl\monks\1nickt>perl elide_sensitive_1.pl ok 1 - '{'password' => 'secret'}' -> '{'password' => '***'}' ok 2 - '{'password' => 'sec\'ret'}' -> '{'password' => '***'}' ok 3 - '{"password" => "secret"}' -> '{"password" => "***"}' ok 4 - '{"password" => "sec\"ret"}' -> '{"password" => "***"}' ok 5 - '{'password' : 'secret'}' -> '{'password' : '***'}' ok 6 - '{'password' : 'sec\'ret'}' -> '{'password' : '***'}' ok 7 - '{"password" : "secret"}' -> '{"password" : "***"}' ok 8 - '{"password" : "sec\"ret"}' -> '{"password" : "***"}' ok 9 - '{'user' => 'secret'}' -> '{'user' => '***'}' ok 10 - '{'user' => 'sec\'ret'}' -> '{'user' => '***'}' ok 11 - '{"user" => "secret"}' -> '{"user" => "***"}' ok 12 - '{"user" => "sec\"ret"}' -> '{"user" => "***"}' ok 13 - '{'user' : 'secret'}' -> '{'user' : '***'}' ok 14 - '{'user' : 'sec\'ret'}' -> '{'user' : '***'}' ok 15 - '{"user" : "secret"}' -> '{"user" : "***"}' ok 16 - '{"user" : "sec\"ret"}' -> '{"user" : "***"}' 1..16 ok 17 - no warnings 1..17


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://1206558]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-06-19 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (111 votes). Check out past polls.

    Notices?