Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Perl script to mask a credit card number

Posted on 2003-12-02
11
Medium Priority
?
1,424 Views
Last Modified: 2012-08-14
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
Comment
Question by:EschbacherG
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 20

Accepted Solution

by:
jmcg earned 324 total points
ID: 9861641
(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
 
LVL 2

Author Comment

by:EschbacherG
ID: 9861880
What if there are no spaces in the cardnumber?

like  123412324123241234?  Can i just remove your spaces from your regular expression?
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 80 total points
ID: 9862716
$credit_card_number=~s((\d{4}( ?)){3})((x x 4) x 3)e;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 20

Expert Comment

by:jmcg
ID: 9863132
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
 
LVL 1

Expert Comment

by:josephfluckiger
ID: 9863934
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
 
LVL 20

Expert Comment

by:jmcg
ID: 9864234
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
 
LVL 1

Expert Comment

by:josephfluckiger
ID: 9864543
thanks, I owe you some points :).

sorry to use your question space EschbacherG.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9865720
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
 
LVL 2

Author Comment

by:EschbacherG
ID: 9867045
Thank you both very much for your quick answers.   In the end, I went with jmcg's suggestion.
0
 

Expert Comment

by:joneoytk
ID: 10721194
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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question