Click here to Skip to main content
15,035,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to do regex for these credit cards in the PHP. •Visa cards have 16 digits and start with a 4 •MasterCard have 16 digits and start with digits 51through to55 •American Express has 15 digits and starts with 34or37.


What I have tried:

i did a regex
/^(51|52|53|54|55)\d+/
but it is not working as I am expecting.
Posted
Updated 25-May-21 10:00am

Try this:
^((?<VISA>4\d{15})|(?<MC>5[1-5]\d{14})|(?<AMEX>3[47]\d{13}))$

If you are going to work with regular expressions, then get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
   
v2
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900