Regex credit card number tests

Remove all , and – and other non-digits from the string first. Then use this regex that matches Visa, MasterCard, American Express, Diners Club, Discover, and JCB cards: ^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$

How can I write a regex which matches non greedy?

The non-greedy ? works perfectly fine. It’s just that you need to select dot matches all option in the regex engines (regexpal, the engine you used, also has this option) you are testing with. This is because, regex engines generally don’t match line breaks when you use .. You need to tell them explicitly that you want to match line-breaks … Read more

Question marks in regular expressions

The key difference between ? and ?? concerns their laziness. ?? is lazy, ? is not. Let’s say you want to search for the word “car” in a body of text, but you don’t want to be restricted to just the singular “car”; you also want to match against the plural “cars”. Here’s an example sentence: I own three cars. Now, if I wanted … Read more