counting the number of characters that were matched in a regularexpression

P

PugetSoundSylvia

Hello all,

Can anyone give me a pointer on this? I'm pretty sure this can be
done, just not sure how.

In a string, I need to replace all instances of 15 or 16 number
characters with the 11 or 12 "x" characters, then the last 4 digits of
the 15 or 16 number characters. The chunk of number characters may
appear numerous times in the string.

For instance, if I have this:

$PotentialCreditCardData = "bac1984938193829382 099824 3s ";

.... I want to have this
$PotentialCreditCardData = "bacxxxxxxxxxxxx9382 099824 3s ";

Here's what I have so far that doesn't work:

use strict;
use warnings;

my($PotentialCreditCardData);

$PotentialCreditCardData = "bac1984938193829382 099824 3s ";
# $PotentialCreditCardData = "junk 198493819382938 22 ";
# $PotentialCreditCardData = "02m5k2 198493819382938 gg24
198493819382938";

$PotentialCreditCardData =~ s/(\d{11,12})(\d{4})/$1$2/g;
print $PotentialCreditCardData;

thanks for any hints!
Sylvia
 
P

PugetSoundSylvia

Actually, this was what I was trying, that doesn't work...

$PotentialCreditCardData =~ s/(\d{11,12})(\d{4})/"X"length($1)$2/g;
 
J

John W. Krahn

Hello all,

Can anyone give me a pointer on this? I'm pretty sure this can be
done, just not sure how.

In a string, I need to replace all instances of 15 or 16 number
characters with the 11 or 12 "x" characters, then the last 4 digits of
the 15 or 16 number characters. The chunk of number characters may
appear numerous times in the string.

For instance, if I have this:

$PotentialCreditCardData = "bac1984938193829382 099824 3s ";

... I want to have this
$PotentialCreditCardData = "bacxxxxxxxxxxxx9382 099824 3s ";

Here's what I have so far that doesn't work:

use strict;
use warnings;

my($PotentialCreditCardData);

$PotentialCreditCardData = "bac1984938193829382 099824 3s ";
# $PotentialCreditCardData = "junk 198493819382938 22 ";
# $PotentialCreditCardData = "02m5k2 198493819382938 gg24
198493819382938";

$PotentialCreditCardData =~ s/(\d{11,12})(\d{4})/$1$2/g;
print $PotentialCreditCardData;

$ perl -le'
my @PotentialCreditCardData = (
"bac1984938193829382 099824 3s ",
"junk 198493819382938 22 ",
"02m5k2 198493819382938 gg24 198493819382938",
);

for my $data ( @PotentialCreditCardData ) {
print $data;
$data =~ s/ (?: (?<=\D) | ^ ) ( [0-9]{11,12} ) (?= [0-9]{4} (?: \D
| $ ) ) / "X" x length( $1 ) /xeg;
print $data;
}
'
bac1984938193829382 099824 3s
bacXXXXXXXXXXXX9382 099824 3s
junk 198493819382938 22
junk XXXXXXXXXXX2938 22
02m5k2 198493819382938 gg24 198493819382938
02m5k2 XXXXXXXXXXX2938 gg24 XXXXXXXXXXX2938



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top