counting the number of characters that were matched in a regular

counting the number of characters that were matched in a regular

am 16.04.2008 21:08:05 von 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

Re: counting the number of characters that were matched in a regular

am 16.04.2008 21:20:13 von PugetSoundSylvia

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

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

Re: counting the number of characters that were matched in a regular expression

am 16.04.2008 21:42:43 von Glenn Jackman

At 2008-04-16 03:08PM, "PugetSoundSylvia@gmail.com" wrote:
> 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.

You want s/(\d{11,12})(\d{4})/"x" x length($1) . $2/eg

Note the 'e' modifier


--
Glenn Jackman
"If there is anything the nonconformist hates worse than a conformist,
it's another nonconformist who doesn't conform to the prevailing
standard of nonconformity." -- Bill Vaughan

Re: counting the number of characters that were matched in a regular expression

am 16.04.2008 21:52:51 von Jim Gibson

In article
,
wrote:

> Actually, this was what I was trying, that doesn't work...
>
> $PotentialCreditCardData =~ s/(\d{11,12})(\d{4})/"X"length($1)$2/g;

This is close, but you need to add the 'x' operator, the 'e' modifier,
and rewrite your replacement string as a valid Perl expression:

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

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
http://www.usenet.com

Re: counting the number of characters that were matched in a regular expression

am 16.04.2008 22:18:52 von someone

PugetSoundSylvia@gmail.com wrote:
>
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall