regex replace credit card numbers with *

Discussion in 'Perl Misc' started by cldmismgr, Sep 29, 2005.

  1. cldmismgr

    cldmismgr Guest

    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";

    Thanks for any comments or advice
    Craig
    cldmismgr, Sep 29, 2005
    #1
    1. Advertising

  2. "cldmismgr" <> 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__

    perldoc perlop for tr.

    Sinan
    --
    A. Sinan Unur <>
    (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
    A. Sinan Unur, Sep 29, 2005
    #2
    1. Advertising

  3. cldmismgr

    Paul Lalli Guest

    A. Sinan Unur wrote:
    > "cldmismgr" <> 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.
    Paul Lalli, Sep 29, 2005
    #3
  4. cldmismgr

    Paul Lalli Guest

    cldmismgr 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;
    > }


    This is just silly. Why are you using an entire for loop just to get a
    repeated character?

    my $star = '*' x (length ($addr) - 4);

    > print "$addr\n";
    > $addr =~ s/^[0-9]{$size}/$star/;


    Or, do it all at once:
    my $size = length($addr) - 4;
    $addr =~ s/^\d{$size}/'*' x $size/e;

    Or, if you're can guarantee to yourself that the string will only have
    digits to begin with:

    substr($addr, 0, $size) = '*' x $size;

    > print "$addr\n$star\n";


    Hope this helps,
    Paul Lalli
    Paul Lalli, Sep 29, 2005
    #4
  5. cldmismgr

    cldmismgr Guest

    I could not make your first suggestion work. I got the string back
    unchanged.
    The second one works great for any size string.
    New light has been shed on the mysterious regex. For me anyway.
    Thanks
    cldmismgr, Sep 29, 2005
    #5
  6. cldmismgr

    Paul Lalli Guest

    Christian Winter wrote:
    > cldmismgr 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.

    >
    > You could use a look-ahead assertion for that:
    > $addr =~ s/\d(?=\s{4})/*/g;


    Small but significant typo - you want to look for 4 digits remaining,
    not spaces.

    $addr =~ s/\d(?=\d{4})/*/g;

    Paul Lalli
    Paul Lalli, Sep 29, 2005
    #6
  7. Paul Lalli <> wrote:
    >
    > A. Sinan Unur wrote:
    >> "cldmismgr" <> wrote in news:1128001319.174650.190970
    >> @g47g2000cwa.googlegroups.com:
    >>
    >> > replace all but the last 4 numbers
    >> > in a credit card number with *'s.



    >> my $cc = '123456789012345';
    >> $cc =~ tr/0123456789/*/;


    > Er, the output of that code is a series of 15 *'s. Doesn't seem to be
    > what the OP asked for.



    So let's fix it with an lvalue substr():

    substr($cc, 0, -4) =~ tr/0-9/*/;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 29, 2005
    #7
  8. cldmismgr

    Paul Lalli Guest

    Tad McClellan wrote:
    > >> "cldmismgr" <> wrote in news:1128001319.174650.190970
    > >> @g47g2000cwa.googlegroups.com:
    > >>
    > >> > replace all but the last 4 numbers
    > >> > in a credit card number with *'s.

    >
    > an lvalue substr():
    >
    > substr($cc, 0, -4) =~ tr/0-9/*/;


    Ooh, I like! I was trying to think of a good way of doing this with
    substr, but I was blanking on remembering the "negative limit" feature
    of the function.

    Thanks, Tad.

    Paul Lalli
    Paul Lalli, Sep 29, 2005
    #8
  9. cldmismgr

    Guest

    cldmismgr <> 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
    , Sep 29, 2005
    #9
  10. "Paul Lalli" <> wrote in
    news::

    >
    > A. Sinan Unur wrote:
    >> "cldmismgr" <> 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 <>
    (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
    A. Sinan Unur, Sep 29, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. redstar

    Credit card processing

    redstar, Jul 11, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,025
    redstar
    Jul 11, 2003
  2. John
    Replies:
    5
    Views:
    634
    Scott Allen
    Dec 15, 2004
  3. soniyaa 1111

    Shop online without credit card

    soniyaa 1111, Mar 6, 2010, in forum: Java
    Replies:
    0
    Views:
    247
    soniyaa 1111
    Mar 6, 2010
  4. soniyaa 1111

    Shop online without credit card

    soniyaa 1111, Mar 6, 2010, in forum: Python
    Replies:
    0
    Views:
    200
    soniyaa 1111
    Mar 6, 2010
  5. Replies:
    3
    Views:
    329
Loading...

Share This Page