LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Weird Regular Expression Notation for Visa Credit Card (https://www.linuxquestions.org/questions/linux-newbie-8/weird-regular-expression-notation-for-visa-credit-card-793855/)

mattseanbachman 03-07-2010 06:40 PM

Weird Regular Expression Notation for Visa Credit Card
 
Hello,

I came across this regular expression via the site http://www.regular-expressions.info/creditcard.html

I can't seem to understand it:

Code:

^4[0-9]{12}(?:[0-9]{3})?$
Specifically, I can't comprehend the part with the '?:'. As far as I'm aware, this wouldn't work because the '?' matches the previous token, which is an opening quote in this case. The colon, by my reasoning, doesn't belong in an expression matching credit cards.

Despite the strange syntax, this appears to work. Anyone have any thoughts on why?

-Matt

neonsignal 03-07-2010 07:05 PM

The '?:' is so that the parentheses don't generate backreferences. This is irrelevant if you are only using grep, but in a language like perl the backreferences are used to capture substrings from the match.

mattseanbachman 03-07-2010 08:04 PM

Alright, thanks for the info. I'll have to look further into what these "back-references" entail. Until now, I've never heard of them. But as you mentioned, I'm mostly just grepping for things and using them in the context of recovering data from a seized hard drive.

Thanks again.

jlinkels 03-07-2010 09:37 PM

Beware, this egular expression is used to identify a string as a possible credit card number, however it does not verify it is a real credit card number. To do that, the algorithm is more complicated, and I doubt whether it can be done with a regular expression.
http://en.wikipedia.org/wiki/Luhn_algorithm is a starting point, more links on that page.

jlinkels


All times are GMT -5. The time now is 04:03 AM.