ip address REGEX
could someone give a regex that detects a VALID ip address:
note: 255.255.255.255 is valid in my book i cant seem to write one. i'm trying to parse a file of valid and invalid IP's and print the good ones, it's using egrep and there is 1 ip per line. i had to do this in school once before but never got the answer. |
dunno, in perl maybe something like this;
#IP is in $ip @ip_bits = split(/\./,$ip); die if(scalar(@ip_bits)<4); foreach(@ip_bits) { $_ =~ s/\D//g; die if($_ > 255 || $_ < 0); } Again, I am not too great with perl, but I think that does it... though it isn't a one-off regexp |
perl...so cryptic looking, i dont know any perl but thanks.
|
wait, i can tell what that does.
im guessing split splits the ip with a "." delimitor and then tests each part to see if it is greater than 255. took a minute but i get it now. |
Python:
Code:
def isIP(s): |
If you really want to use egrep:
Code:
egrep '^ *(([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5]) *$' your_file.txt Also note that it accepts zero-padding, i.e. 001.002.003.004 will be valid. but this actually is a valid IP-address. |
I just did this the other day, try it out (perl):
1) Make some variables for the regex pattern (let me know if it breaks ;) Code:
# Code:
if(grep(/$ip/,$some_string)){print 'you get the point';} |
Quote:
|
thanks guys. the egrep example is the one i really needed.
im not using perl or python so i needed just a plain ole regex, but i will save all of the example becuase you never know. i will have to use python later on tho....have to learn it to teach it to my girlfriend because she has to take a class that uses it. i get to learn it for the sake of helping her out in the class (arent i a good boyfriend). |
perl will do it great. the example posted was correct
|
the egrep one works also by the way....so that's atleast 2 that have been tested.
|
does the python example work? wouldnt 123.123.123.123.123 pass? i cant see where it checks for the number of octets.
<edit>also, how does int() treat characters, if someone entered 46.abc.123.87, what would int(abc) evaluate to? hopefully not 0 because if it did then it would pass. but i have to say, not knowing python/perl and not being very good at regex, the python example was the only one i could read, and i wouldnt want to have to maintain the others. |
Quote:
Quote:
Quote:
Here's the modified version to check for number of octets as well. Code:
def isIP(s): |
Re: ip address REGEX
The answers above are all very well, but doesn't fully solve the problem :p
Quote:
@ipbits = split(/\./); #!/usr/bin/perl -w main: while(<>) { @ipbits = /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})?/; # pick out ip addresses next unless (@ipbits); # verify pattern match for (@ipbits) { next main if ($_ <0 || $_>255); # verify ranges } print join('.',@ipbits)."\n"; # print valid numbers } The important things to know in order to read perl is: 1) Scalar variables are prefixed with $, arrays with @ and hashes with # 2) Many perl functions work implicitly on $_ if nothing else is stated. 3) one-line if are written in reverse. E.g.: die if ($shot); |
Quote:
Quote:
thanks for the perl hints though. |
All times are GMT -5. The time now is 12:59 AM. |