Re: regex replace credit card numbers with *
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Thu, 29 Sep 2005 21:37:42 GMT
"Paul Lalli" <mritty@xxxxxxxxx> wrote in
news:1128005076.901853.11560@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:
>
> A. Sinan Unur wrote:
>> "cldmismgr" <admin@xxxxxxxxxx> wrote in news:1128001319.174650.190970
>> @g47g2000cwa.googlegroups.com:
>>
>> > 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";
>>
>> Well, do you just have a string of nine digits, or is there anything
>> special about the way the credit card number is formatted?
>>
>> use strict ;
>> use warnings;
>>
>> my $cc = '123456789012345';
>> $cc =~ tr/0123456789/*/;
>>
>> print "$cc\n";
>>
>> __END__
>>
>
> Er, the output of that code is a series of 15 *'s. Doesn't seem to be
> what the OP asked for.
Sorry, I missed that part.
One way to do that with the substitution operator is:
use strict ;
use warnings;
my $cc = '123456789012345';
$cc =~ s/\d{11}(?=\d{4})/('*') x 11/e;
print "$cc\n";
__END__
D:\Home\asu1\UseNet\clpmisc> c
***********2345
Sinan
PS: I am assuming that the OP incorrectly stated that he wanted replace
the first 9 digits and leave the last four alone because the string
contains 15 characters.
--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
.
- References:
- regex replace credit card numbers with *
- From: cldmismgr
- Re: regex replace credit card numbers with *
- From: A. Sinan Unur
- regex replace credit card numbers with *
- Prev by Date: Re: regex replace credit card numbers with *
- Next by Date: hex-array print
- Previous by thread: Re: regex replace credit card numbers with *
- Next by thread: Re: regex replace credit card numbers with *
- Index(es):
Relevant Pages
|