Re: regex replace credit card numbers with *
- From: axel@xxxxxxxxxxxxxxxxxxxxxx
- Date: Thu, 29 Sep 2005 21:29:39 GMT
cldmismgr <admin@xxxxxxxxxx> wrote:
> I have this little piece of code to replace all but the last 4 numbers
> in a credit card number with *'s. Is there a better way to do this? I
> tried using just a regex but everything I tried failed. For example
> s/^[0-9]{9}/*/ replaced this first 9 digits with one *. The idea is to
> replace each digit with an asterick and leave the last four digits
> alone.
> $st = "*";
> $addr = '123456789012345';
> $size = length($addr) - 4;
> for ($i=0;$i < $size; $i++) {
> $star .= $st;
> }
> print "$addr\n";
> $addr =~ s/^[0-9]{$size}/$star/;
> print "$addr\n$star\n";
One solution would be as follows - note also allows for non-digits
to be interspersed (and ignored).
#!/usr/bin/perl
use strict;
use warnings;
my $st = "*";
my $addr = '1245-4536-8904-2345';
print $addr, "\n";
$addr =~ s/\d/$st/ for (1 .. ($addr =~ tr/0-9//) - 4);
print $addr, "\n";
__END__
Output:
1245-4536-8904-2345
****-****-****-2345
Axel
.
- References:
- regex replace credit card numbers with *
- From: cldmismgr
- regex replace credit card numbers with *
- Prev by Date: installing modules?
- Next by Date: Re: regex replace credit card numbers with *
- Previous by thread: Re: regex replace credit card numbers with *
- Next by thread: FAQ 9.25 How do I fetch/put an FTP file?
- Index(es):
Relevant Pages
|
|