LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-07-2010, 07:40 PM   #1
mattseanbachman
Member
 
Registered: Feb 2010
Posts: 40

Rep: Reputation: 15
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

Last edited by mattseanbachman; 03-07-2010 at 07:42 PM. Reason: cleared up ambiguity
 
Old 03-07-2010, 08:05 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Jessie (Fluxbox WM)
Posts: 1,387
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
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.

Last edited by neonsignal; 03-07-2010 at 08:07 PM.
 
1 members found this post helpful.
Old 03-07-2010, 09:04 PM   #3
mattseanbachman
Member
 
Registered: Feb 2010
Posts: 40

Original Poster
Rep: Reputation: 15
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.
 
Old 03-07-2010, 10:37 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Visa to Turn Android, Nokia Phones Into Credit Cards LXer Syndicated Linux News 0 09-27-2008 07:50 AM
convert a logical expression to reverse Polish notation in Java manolakis Programming 9 08-25-2008 08:45 AM
Need help with a regular expression abefroman Programming 2 05-28-2008 10:17 PM
regular expression in lex's notation browneyes Programming 1 10-04-2004 05:43 PM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 09:13 AM


All times are GMT -5. The time now is 02:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration