in reply to Match any characters

Is this what you are after:

use warnings; use strict; while (<DATA>) { chomp; s/ (logger\s+) ("[^"]*"|\w+)(,)([^,]+)(.*) /$1 . ("X" x length($2)) . $3 . ("X" x length($4) . $5)/ex; print "$_\n"; } __DATA__ logger abcdef123,"$Ł*&GHi^ logger "765)(?>jh",hhhhhh,joebloggs logger "7,)(?>jh",yyyyyyy,fredbloggs

Prints:

logger XXXXXXXXX,XXXXXXXXX logger XXXXXXXXXXX,XXXXXX,joebloggs logger XXXXXXXXXX,XXXXXXX,fredbloggs

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Match any characters
by Anonymous Monk on Feb 10, 2006 at 10:28 UTC
    Thanks. I've just discovered a slight variation on the theme in the log I'm interrogating which will need handling however.
    It seems that the first element after logger is not always a password. This field may be alphanumeric and possibly include the @ # Ł $ and _ characters (but no others) and may not be surrounded by quotes e.g.
    logger abc@def,"werty^%$&" logger ab$Łef,12trsgh logger "765)(?>jh",hhhhhh,joebloggs logger "7,)(?>jh",yyyyyyy,fredbloggs
    Where the output should be
    logger abc@def,XXXXXXXXX logger ab$Łef,XXXXXXX logger XXXXXXXXXXX,XXXXXX,joebloggs logger XXXXXXXXXX,XXXXXXX,fredbloggs
    How do I adapt the code to handle that ?

      Cleaner to do that with a number of matches:

      use warnings; use strict; use Data::Dumper; while (<DATA>) { if (/logger\s+"([^"]*)",(\w+),(.*)/s) { print "logger " . ('X' x length $1) . ',' . ('X' x length $2) +. ",$3"; } elsif (/logger\s+([^,]*),"([^"]*)"(,?.*)/s) { print "logger $1," . ('X' x length $2) . "$3"; } elsif (/logger\s+([^,]*),(\w+)(,?.*)/s) { print "logger $1," . ('X' x length $2) . "$3"; } } __DATA__ logger abc@def,"werty^%$&" logger ab$Łef,12trsgh logger "765)(?>jh",hhhhhh,joebloggs logger "7,)(?>jh",yyyyyyy,fredbloggs

      Prints:

      logger abc@def,XXXXXXXXX logger ab$Łef,XXXXXXX logger XXXXXXXXX,XXXXXX,joebloggs logger XXXXXXXX,XXXXXXX,fredbloggs

      DWIM is Perl's answer to Gödel
        Many thanks