[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Regex Question



On Thu, Oct 21, 2004 at 10:44:52AM -0500, Diego Gardel wrote:
> Hello,
> I am working on creating a regex to match the various credit card numbering 
> patterns. I am unsure of my syntax. I believe it to be correct, however in 
> one regex syntax verification application I am using, I receive an error 
> stating that there is an invalid use of a repeat. I tried to verify each 
> card's regex, and received the same response. Here is the regex I am using 
> for MasterCard patterns.
> 
> ^5[1-5]\d{2}(?:(?:\s|\-)?\d{4}){3}$
> 
> It should match a pattern of
>   xxxx-xxxx-xxxx-xxxx
>   xxxx xxxx xxxx xxxx
>   xxxxxxxxxxxxxxxx
> 
> The first digit will be a 5, the second will be from a range of 1-5.

Since you're just using single chars in that "or" group, you can save
some space (and legibility, IMHO) with

^5[1-5]\d\d(?:[\s\-]?\d{4}){3}$

I also prefer to just repeat the \d rather than using {2}, personally,
but that's just my preference.  Your syntax looks correct.  That's only
gonna match a whole line that contains nothing but a CC number, though -
or you're gonna have to have tokenized the line first.  The tokenization
may prove to be a bit of a pain, given that you're allowing spaces or
hyphens in the string.  If you're just scanning files on a machine,
looking for credit card numbers, you'll wanna take the ^ and $ off, mash
the file into one big line, and use /m to find the number groups (in case
they're split across multiple lines for some reason).

Now stay away from my machines (not that I keep financial information
anywhere it could be found, anyway). :)

--Danny, who's been away from the list for a bit too long :)

-
To unsubscribe, send email to majordomo@luci.org with
"unsubscribe luci-discuss" in the body.