Welcome Guest, Not a member yet? Register   Sign In
Regex valid ip
#1

[eluser]Twisted1919[/eluser]
yes i know that this is not the first nor the last discussion about validating an ipv4 address with regex .

Currently i have this pattern :
(On many lines for good reading)

Code:
$pattern = '/^(([1-9](0)?\.|[1-9]{2}\.|(1[0-9][0-9]|2[0-5][0-5])\.)';
$pattern.= '([0-9]{1,2}\.|[1-9]{2}\.|(1[0-9][0-9]|2[0-5][0-5])\.)';
$pattern.= '([0-9]{1,2}\.|[1-9]{2}\.|(1[0-9][0-9]|2[0-5][0-5])\.)';
$pattern.= '([0-9]{1,2}|[1-9]{2}|(1[0-9][0-9]|2[0-5][0-5])))$/';

yes , a long one , so i was wondering if you can help me with something similar that will allow only valid ip addreses but without the help of php , just regex .

I'm sure that must be a way of doing this , but i am just learning regex , and this is all that i could come up with .

Thanks.
#2

[eluser]sophistry[/eluser]
here is a thread that talks about CI's valid_ip() function.

maybe that will help?
#3

[eluser]Twisted1919[/eluser]
Not quite , i need a regex "standalone" solution . i don't want to use php at all , exception for preg_match() at the end .
Example:
Code:
<?php
if( preg_match('/A Pattern that will match all ips classes/',$ip) )
{
echo 'congrats your ip passed our filters'."\n";
}
else
{
echo 'are u serious ?';
}
?>
#4

[eluser]slowgary[/eluser]
That thread DOES have a regex in it somewhere. Looked like this:
Code:
if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $ip))
{
     return FALSE;
}

But you shoudlread the thread and check out the rest of the code so you know what the regex won't do (although it may be suitable as stands). I'm not too good with regexes myself (and probably never will be ;-P).
#5

[eluser]Twisted1919[/eluser]
slowgary , your provided regex is no good at all .
Passing : 192.168.2.299 returns true , but really is ?
or : 0.123.123.123 , is this a real ip ?

On the previus post , i said that i want something more accurate but only from regex .
My pattern does the job pretty good , as it finds that ip like 0.123.123.123 or like 192.168.2.299 is not a real ip BUT i need some minimized version for that regex .
#6

[eluser]slowgary[/eluser]
You must've missed the part of my post where I mentioned that the regex is from sophisty's post, and that I don't have much experience in the regex realm.

Why is 192.168.2.299 not a real IP? Looks real to me. In fact, I just might assign that IP to my machine right now ;-P
#7

[eluser]Twisted1919[/eluser]
Ok , as far as i know , maximum number from an ip block can't be more than 255 . so , something like 255.255.255.255 wolud be correct , but something like 255.255.255.256 wouldn't be .

I didn't miss that part , just that i didn't find any solution there ...
#8

[eluser]slowgary[/eluser]
You're right, I was just being facetious. This seems like it'd be a common problem though, so I thought it a good idea to take google for a ride. I was feeling lucky and came up with this:

http://www.regular-expressions.info/examples.html

They have a few different regexes for IP addresses and they all seem to be shorter than your "Fat Aunt Bertha" up above. I hope one of those works for you.
#9

[eluser]Twisted1919[/eluser]
It's a good start , i see it's a complete site for regular expressions . I'm sure i'll find the answer there .
Thank you Wink
#10

[eluser]sophistry[/eluser]
@twisted... why does it have to be regex... is this for a homework assignment?




Theme © iAndrew 2016 - Forum software by © MyBB