Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Looking for a certain regexp

Reply
Thread Tools

Looking for a certain regexp

 
 
Robert TV
Guest
Posts: n/a
 
      06-29-2004
Hi, has onyone ever created a regexp to match common credit card formulas? I
have been asked to "validate" some CC#'s we have on file. I do not know the
formulas by which credit cards are generated. All I know is Visa always
starts with 4, Mastercard with 5, and Amex with 3 I think. It would be nice
to have:

$cardtype = param('cardtype');
$cardnumber = param('cardnumber');

if ($cardtype eq "Visa") {
unless ($cardnumber =~ /some valid visa formula/) {
print "It's Good";
} else {
print "It's Bad";
}
}

And so forth. You can find e-commerce websites all over the place that do a
basic check but I don't know the formulas used. At the very least, I would
like to validate the first number as being 4,5,3 etc. and that the scalar is
at least 13 characters long. I do not know how to match just the very first
char of a scalar ... can some one help me with this? EG:

$cardtype = param('cardtype');
$cardnumber = param('cardnumber');

if ($cardtype eq "Visa") {
unless ($cardnumber =~ /first character is 4/ && $cardnumber =~ /at least
13 characters long/) {
print "It's Good";
} else {
print "It's Bad";
}
}

I am very new to writing regexp's and my present knowledge is limited to
validating characters used, not their positions. This was my best guess to
match 4 as first (untested):

unless ($cardnumber =~ /4.*/) { #does not match character length as 13
however
print "It's Good";

} else {
print "It's Bad";
}

Thank you all.
Robert


 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      06-29-2004
Robert TV wrote:
> Hi, has onyone ever created a regexp to match common credit card
> formulas? I have been asked to "validate" some CC#'s we have on file.
> I do not know the formulas by which credit cards are generated.


Why aren't you simply using the Business::CreditCard module?

jue


 
Reply With Quote
 
 
 
 
Robert TV
Guest
Posts: n/a
 
      06-29-2004
"Robert TV" <> wrote:

> unless ($cardnumber =~ /4.*/) { #does not match character length as 13
> however
> print "It's Good";
>
> } else {
> print "It's Bad";
> }


I just wrote this regexp that seems to correctly match the first number as
4, although i'm not certain if it's the best way:

#!/usr/bin/perl

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

$thenumber = "46574656";

if ($thenumber =~ /^[4].*$/) {
print "It's Good";
} else {
print "It's Bad";
}
exit;

Now if I could only figure out how to match against a certain number if
characters (13) in the scalar. Your suggestions are appreciated.

Robert


 
Reply With Quote
 
Robert TV
Guest
Posts: n/a
 
      06-29-2004
"Jürgen Exner" <> wrote:

> Why aren't you simply using the Business::CreditCard module?


I do not own my own server or have the ability to install such modules that
are not bundled with Perl5. I have an account with a commercial web hosting
company and a cgi-bin to run my scripts. They will not install custom
modules for me : (

Robert


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-29-2004
Robert TV wrote:
> Jürgen Exner wrote:
>> Why aren't you simply using the Business::CreditCard module?

>
> I do not own my own server


Okay.

> or have the ability to install such modules that are not bundled
> with Perl5.


Yes, you have.

> I have an account with a commercial web hosting company and a
> cgi-bin to run my scripts. They will not install custom modules for
> me : (


Have you asked? If they say no, then do it yourself.

perldoc -q "own module"

http://www.mail-archive.com/beginner.../msg59382.html

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Eric Schwartz
Guest
Posts: n/a
 
      06-29-2004
"Robert TV" <> writes:
> Hi, has onyone ever created a regexp to match common credit card formulas?


If you were to search for "Credit Card" on http://search.cpan.org/
you'd find Business::CreditCard which claims to

Validate/generate credit card checksums/names

You know, it's kinda rude to ask other people to search for you, when
you can just do the same thing yourself.

> I have been asked to "validate" some CC#'s we have on file.


Do you know what "validate" means? Are you supposed to make sure
they're real credit card numbers, or just that they might be?
Business::CreditCard doesn't really validate cards; it just makes sure
that the credit card number is vaguely plausible.

> I am very new to writing regexp's and my present knowledge is limited to
> validating characters used, not their positions.


You should read 'perldoc perlretut', then. 'perldoc perlrequick' if
you're in a hurry.

> This was my best guess to
> match 4 as first (untested):


Why? You can test it yourself, easily.

> unless ($cardnumber =~ /4.*/) { #does not match character length as 13


That will match a string with a literal '4' in it anywhere.

> however


That will cause a syntax error.

> print "It's Good";
>
> } else {
> print "It's Bad";
> }


Sounds like you've got an idea that regexes can do anything; they can
do a lot, but they're not ideal for everything. Also, sometimes it's
clearer to express yourself not using a regex. For instance, if what
you're concerned with is to find the length of a string, use the
length() function ('perldoc -f length' to find out more).

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
 
Reply With Quote
 
Daedalus
Guest
Posts: n/a
 
      06-30-2004
> I just wrote this regexp that seems to correctly match the first number as
> 4, although i'm not certain if it's the best way:
>
> #!/usr/bin/perl
>
> use CGI qw(:standard);
> use CGI::Carp qw(fatalsToBrowser);
>
> $thenumber = "46574656";
>
> if ($thenumber =~ /^[4].*$/) {
> print "It's Good";
> } else {
> print "It's Bad";
> }
> exit;
>
> Now if I could only figure out how to match against a certain number if
> characters (13) in the scalar. Your suggestions are appreciated.
>


>At the very least, I would
>like to validate the first number as being 4,5,3 etc. and that the scalar

is
>at least 13 characters long. I do not know how to match just the very first
>char of a scalar ... can some one help me with this? EG:
>


Instead of the * quantifier, you could use {12,} who will match 12 or more
charaters (plus the leading 4 = 13).

/^4.{12,}/ #but that would match much more than digits

Using character classes might be the answer

/^4[\d]{12,}/ # should match a string that begin with 4 plus 12 digit

DAE


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-30-2004
Robert TV wrote:
> "Jürgen Exner" <> wrote:
>
>> Why aren't you simply using the Business::CreditCard module?

>
> I do not own my own server or have the ability to install such
> modules that are not bundled with Perl5.


Option 1: perldoc -q module: "How do I keep my own module/library
directory?"
Option 2: instead of reinventing the wheel just copy the (relevant subset
of) code from that module into your own program

jue


 
Reply With Quote
 
Robert TV
Guest
Posts: n/a
 
      06-30-2004
"Gunnar Hjalmarsson" <> wrote

> Have you asked? If they say no, then do it yourself.
>
> perldoc -q "own module"
>
> http://www.mail-archive.com/beginner.../msg59382.html


Thank you Gunnar, that tutorial helped me install a .pm on the server and it
works great.

Robert


 
Reply With Quote
 
Robert TV
Guest
Posts: n/a
 
      06-30-2004
"Eric Schwartz" <> wrote

> You know, it's kinda rude to ask other people to search for you, when
> you can just do the same thing yourself.


I didn't want people to do my work for me, I really didn't know where to
start with my research. I know very little about CPAN but am starting to
learn its a good resource.

> Do you know what "validate" means? Are you supposed to make sure
> they're real credit card numbers, or just that they might be?
> Business::CreditCard doesn't really validate cards; it just makes sure
> that the credit card number is vaguely plausible.


I now have CPAN's CC validity module working.

> You should read 'perldoc perlretut', then. 'perldoc perlrequick' if
> you're in a hurry.


I'll have a look ty.
R


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Vista cannot obtain an IP address from certain routers or from certain non-Microsoft DHCP Brian W Wireless Networking 7 01-31-2010 03:46 AM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Binding certain rows to certain columns in GridView? bernard.oflynn@gmail.com ASP .Net 2 03-25-2008 03:49 PM
Finding one certain line in a file is easy but how to look forheadlines and something just under this certain headline? kazaam Ruby 3 08-26-2007 03:34 PM
Expanding certain path to certain node in a JTree arun.hallan@gmail.com Java 0 01-08-2005 08:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57