[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1452
  • Last Modified:

Perl script to mask a credit card number

Here's the basic idea:  given a 16 digit credit card number, I want to replace the first 12 digits with x's.

Example:  1223 1223 1234 2345  would become   xxxx xxxx xxxx 2345

I'm using ActiveState Perl.


Thanks for any assistance!
0
EschbacherG
Asked:
EschbacherG
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
jmcgOwnerCommented:
(my $credit_card_number_masked = $credit_card_number) =~ s/.*(\d{4})$/xxxx xxxx xxxx $1/;

This just picks off and preseres the last four digits, disregarding whatever comes before.
0
 
EschbacherGAuthor Commented:
What if there are no spaces in the cardnumber?

like  123412324123241234?  Can i just remove your spaces from your regular expression?
0
 
ahoffmannCommented:
$credit_card_number=~s((\d{4}( ?)){3})((x x 4) x 3)e;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jmcgOwnerCommented:
If preserving the spaces (or lack of spaces) is important, you'll have to do something more like what ahoffman is showing you. If you want the x-ed-out credit card number to look a particular way, however, you're better off using my approach.
0
 
josephfluckigerCommented:
Hope you don't mind me asking a question here, but what does the "e" at the end of the expression do?

$credit_card_number=~s((\d{4}( ?)){3})((x x 4) x 3)e; <----
0
 
jmcgOwnerCommented:
An "e" modifier on a substitution indicates that the right hand side of the substitution is to be interpreted as a perl expression rather than as a double-quoted string literal (well, nearly). You can even put extra "e"s there to get multiple levels of expression interpretation, but I've never seen this usefully done more than two levels deep.
0
 
josephfluckigerCommented:
thanks, I owe you some points :).

sorry to use your question space EschbacherG.
0
 
ahoffmannCommented:
well, my suggestion is not perfect, and to be improved in many ways (as jmcg's too:), but it answers the question
KISS - keep it stupid simple  ;-)

josephfluckiger, probably you replace
  x x 4
in my suggestion by
  X x 4
to understand what happens
  print "X" x 4
characters are just characters, and some have special meaning in special context ..
0
 
EschbacherGAuthor Commented:
Thank you both very much for your quick answers.   In the end, I went with jmcg's suggestion.
0
 
joneoytkCommented:
so what if the input from keyboard and i dont want it to show on the screen.does this command (my $credit_card_number_masked = $credit_card_number) =~ s/.*(\d{4})$/xxxx xxxx xxxx $1/;
work on my concept too?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now