Re: how to make a regex for a ip address
[EMAIL PROTECTED] (Perl-Krackedpress) writes: // check the email fields for validity ^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$ That will invalidate lots of proper email addresses. For example anything in the .info domain. For example [EMAIL PROTECTED] is also a valid email address (which your regexp won't allow). // check zipcodes for validity - but not the last 4 business digits (^[0-9]{5})-([0-9]{4}$), trim($zip_code)) (!ereg(^[a-zA-Z][0-9][a-zA-Z][[:space:]][0-9][a-zA-Z][0-9]$ // check phone for validity (^(.*)[0-9]{3})(.*)([0-9]{3})(.*)([0-9]{4}$) I see you are not planning to do much business with people outside Canada or the US. How about people with an extension? Checking phone numbers, street addresses and zipcodes for validity will pretty much never work. It's better to assume that the person entering the information know how it's best expressed. Let me include a related joke I posted on the Los Angeles Perl Mongers list a few days ago when someone tried coming up with similar regexp's. Recently, a worldwide survey was conducted by the U.N. The question was asked, Would you please give your opinion about the food shortage in the rest of the world? The survey was a huge failure. In Africa they did not know what 'food' meant. In Western Europe, they did not know what 'shortage' meant. In Eastern Europe they did not know what 'opinion' meant. In South America they did not know what 'please' meant. And in the U.S. they did not know what 'the rest of the world' meant. ;-) - ask -- ask bjoern hansen, http://www.askbjoernhansen.com/ !try; do(); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: how to make a regex for a ip address
On Mon, 19 Aug 2002 12:07:21 -0700, [EMAIL PROTECTED] (Root) wrote: for a one liner: $_ = '12.34.56.78'; map {$_ 256} /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g || warn(not valid ip: $_\n); I tried this with $_ = '1234.2345.56.78'; and received no warning. That's not good. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: how to make a regex for a ip address
er.. see correction -Original Message- From: Angerstein [mailto:[EMAIL PROTECTED]] Sent: Tuesday, August 20, 2002 9:00 AM To: [EMAIL PROTECTED] Subject: AW: how to make a regex for a ip address What about: /\d?\d?\d\.\d?\d?\d\.\d?\d?\d\.\d?\d?\d\/ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ or @ip = split (/\./); foreach $part (@ip) { if ( $part 255 $part =~ /\d?\d?\d\/ ) { if ( $part 255 $part =~ /^\d{1,3}$/ ) { die That´s not an IP; } } The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: how to make a regex for a ip address
Zentara wrote: On Mon, 19 Aug 2002 12:07:21 -0700, [EMAIL PROTECTED] (Root) wrote: for a one liner: $_ = '12.34.56.78'; map {$_ 256} /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g || warn(not valid ip: $_\n); I tried this with $_ = '1234.2345.56.78'; and received no warning. That's not good. the following should be more readable: $_ = 1.2.3.4; (/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ $1 0 $1 256 $2 0 $2 256 $3 0 $3 256 $4 0 $4 256) || warn(Invalid ip: $_\n); david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: how to make a regex for a ip address
Alex Chen wrote at Sun, 18 Aug 2002 17:14:21 +0200: i am a beginners in perl.i am studying perl regex now ,and i want to know how to make a regex for a ip address. Have a look to the Regexp::Common module from the CPAN. $RE{net}{IPv4} contains the regexp to match an ip address. Best Wishes, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: how to make a regex for a ip address
I hope you have a good REF. for regex commands O'Reilly's series book is the best I am told. I do not have one for the IP address but I have email, zipcode, and phone Here are the strings for regex for the following // check the email fields for validity ^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$ // check zipcodes for validity - but not the last 4 business digits (^[0-9]{5})-([0-9]{4}$), trim($zip_code)) (!ereg(^[a-zA-Z][0-9][a-zA-Z][[:space:]][0-9][a-zA-Z][0-9]$ // check phone for validity (^(.*)[0-9]{3})(.*)([0-9]{3})(.*)([0-9]{4}$) Timothy Lungstrom Kracked Press Productions [EMAIL PROTECTED] ** - Original Message - From: Janek Schleicher [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Sunday, August 18, 2002 1:24 PM Subject: Re: how to make a regex for a ip address Alex Chen wrote at Sun, 18 Aug 2002 17:14:21 +0200: i am a beginners in perl.i am studying perl regex now ,and i want to know how to make a regex for a ip address. Have a look to the Regexp::Common module from the CPAN. $RE{net}{IPv4} contains the regexp to match an ip address. Best Wishes, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: how to make a regex for a ip address
Just in case you have trouble understanding some of the examples given, I'll through out a starter regex and you can see how the process kind of works. my $ip = $ARGV[0]; my $badoctet = 0; if($ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){ for($1,$2,$3,$4){ #right format so far... if($_ =255 $_ = 0){ #is it between 0 and 255? print .; }else{ $badoctet = 1; #just so we can check later... } } if($badoctet){ print One of your octets was out of range. Bad IP.\n; }else{ print The IP address seems ok.\n; } }else{ print I don't think that's a valid IP.\n; #wasn't even in the right format. } If you look through the regex, it is checking for four sets of one to three digits (\d{1,3}), separated by a period. Because each set of digits is encased in parentheses, Perl stores the result of that part of the match in variables called $1,$2, etc., so you can later check real quick to make sure that each set of digits is between 0 and 255. I don't recommend using this in a production environment, mind you, because there are probably all sorts of issues with IP addresses that I haven't even thought of, so if you're really looking to put this into some industrial grade code, you should use or look at the source code to a module from CPAN. I'm sure there's a bunch for this. -Original Message- From: perl-Krackedpress [mailto:[EMAIL PROTECTED]] Sent: Sunday, August 18, 2002 9:12 PM To: Janek Schleicher Cc: [EMAIL PROTECTED] Subject: Re: how to make a regex for a ip address I hope you have a good REF. for regex commands O'Reilly's series book is the best I am told. I do not have one for the IP address but I have email, zipcode, and phone Here are the strings for regex for the following // check the email fields for validity ^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$ // check zipcodes for validity - but not the last 4 business digits (^[0-9]{5})-([0-9]{4}$), trim($zip_code)) (!ereg(^[a-zA-Z][0-9][a-zA-Z][[:space:]][0-9][a-zA-Z][0-9]$ // check phone for validity (^(.*)[0-9]{3})(.*)([0-9]{3})(.*)([0-9]{4}$) Timothy Lungstrom Kracked Press Productions [EMAIL PROTECTED] ** - Original Message - From: Janek Schleicher [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Sunday, August 18, 2002 1:24 PM Subject: Re: how to make a regex for a ip address Alex Chen wrote at Sun, 18 Aug 2002 17:14:21 +0200: i am a beginners in perl.i am studying perl regex now ,and i want to know how to make a regex for a ip address. Have a look to the Regexp::Common module from the CPAN. $RE{net}{IPv4} contains the regexp to match an ip address. Best Wishes, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: how to make a regex for a ip address
Emmm... So do you want to do it in 1 line or your just want to try on ? For me, it seems quite non-effective impossible with one line... but breaking them into pieces, that becomes handy. I use this : my $ip = $ENV{REMOTE_ADDR}; my $error = 0; my @seg = split /\./, $ip; $error = 1 unless ($#seg ==3); # or (scalar(@seg)== 4), this check Format error foreach my $num(@seg) # Checking on each segment { $error = 1 if ($num 0 or $num 255 or $num =~ /[^\d]/) # first 2 condition checking the range. # =~ /[^\d]/ means, if found any non digit char. } print $error; # basically like this, but, perhaps you don't want some subnet # mask or localhost or local lan ips... so you can continue # like this : $error = 1 if ($ip =~ /^0|255|192|127\..+$/); # And you can add up some more ... Refer to some existed modules may much helpful too. Rgds, Connie - Original Message - From: alex chen [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Sunday, August 18, 2002 11:14 PM Subject: how to make a regex for a ip address hi, i am a beginners in perl.i am studying perl regex now ,and i want to know how to make a regex for a ip address. thank you!! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]