aspose file tools*
The moose likes Beginning Java and the fly likes IP address regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "IP address regular expression" Watch "IP address regular expression" New topic
Author

IP address regular expression

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2627
    
    9

Hello all, I have a question about IP address regular expression. We all know IP addresses are xxx.xxx.xxx.xxx so I have a Pattern, how do I make sure the xxx is within 0 and 255 inclusive?



I got the up to 3 digits part right I know that. I tried instead of returning immediately, I put the input string into an array and check the range but no luck. Something like but the problem with this is I'm not getting the output looping through the array.



OK I got it to work now. It was with my split() line I originally put in "." instead of "\\." Now it works.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

K. Tsang wrote:how do I make sure the xxx is within 0 and 255 inclusive?

Break down the problem, Lets check only for single number(octet) only,
check for three digits, in range 0 to 5, sequentially, x->x->x
This will help,


OR
Its a 4 byte number, separated by DOT, each number is unsigned 1 byte, returning into 0 - 255, may be some binary operations can help us to validate each field ?


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Might be quicker to split the number on \. (probably written as "\\.") with the java.lang.String#split method, check the resultant array has 4 members, parse each to an int with the java.lang.Integer#parseInt method, check that int is >= 0 and < 0x100 (decimal = 256).
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

That would be easiest, not requiring any RE
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2627
    
    9

I did exactly what Campbell did - split in using "\\." then check each array element.

Earlier when I first did it, it used "\." for the split method which wasn't right. Also the pattern is "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" and not "\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}" which I had at the very beginning.

Of course I'm sure it's possible to have a pattern that also does the validation but that would take some time.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
from a simple google search:



Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I don't think that last regex is correct. I suspect it has a subtle error in. I shall leave you to work out what.

Beware of regular expressions you download from websites. Make sure to test them carefully, because they might have subtle mistakes in.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Yes you are right, I just copied the first result of google without even looking at it :-). anyway I am pretty sure that you will find many many regex there on the internet ready to be used.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Have you tested it? Will it match 127.0.0.1? Will it match 93.32.37.194 (see your IP on this thread)? Will it match 255.255..255? Will it match 127.00.0.1?

I haven't tested it.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Campbell Ritchie wrote:Have you tested it? Will it match 127.0.0.1? Will it match 93.32.37.194 (see your IP on this thread)? Will it match 255.255..255? Will it match 127.00.0.1?

I haven't tested it.


I just told you that you are right, it dosen't work. take it easy
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

Also, IPv6?
You might want to check out java.net.InetAddress (Inet4Address / Inet6Address) if you haven't already.
Won't be of any use if all you're really interested in is a regular expression for IPv4 address validation, though.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
omar al kababji wrote:I just told you that you are right, it dosen't work. take it easy
It is unusual to be told I was right; I shall make the most of this

Where did it go wrong, and (so as to pretend to be educational ) have you worked out why?
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Pattern pattern = Pattern.compile("(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)");

ok this is a tested one ;-)

[edit]Disable smilies. CR[/edit]
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2627
    
    9

It looks like I'm making this thread topic quite a topic.

The regex Omar had doesn't work I know. I once google this and copy a random regex ... put it in my app. Get false all the time.

Jelle mentioned IPv6. In fact I'm just working with IPv4. Because I used java.net.InetAddress getLocalAddress to get the current computer IP and this IP is IPv4. But having a regex for checking IPv6 is indeed good to have. Maybe you can give it try.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
When you tested that regular expression, what did it get wrong? This is where we learn more about regular expressions. Let's see if we can get something sorted out before Piet Verdriet appears on the scene
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2627
    
    9

I found this expression:
"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$"

This looks like it works I tested a few. Yet if the IP is "0.xxx.xxx.xxx" it doesn't work. The first set of digits must be 1 to 255 to work. But can IP start with 0.xxx.xxx.xxx?? Since I no networking dude. HAHA
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
no the pattern is right but you have to use the correct check, the idea is to check that you have a number between 0-255 including the 0 and the 255. and here is a part of the pattern that checks this situation. the rest is just to check that this gets replicated four times with the dots between them.




in the code you have to be sure that you find one and only one match and this match is exactly the whole string, for example if you have the number 553 its not a valid one, however using the pattern you get a match on the (55) through the last part of the regex which is [0-1]?[0-9][0-9]? and then another 3 by the same regex part [0-1]?[0-9][0-9]? however since in this case you got two matcher you are pretty sure that its not valid. so you have to check this double situation.

1) a match found
2) there is only one match.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
to be more accurate when you use the pattern i passed you have to check the string containing the ip address like this

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Have you tried it with 127.0.0.1, 127..0.1 and 127.00.0.1?
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Yes it works. did you tried it ??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Is 127.0.00.1 a valid IP? I have never seen a double 0 like that.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Is 127.0.00.1 a valid IP? I have never seen a double 0 like that.


well open your terminal/cmd and type:

ping 127.0.00.1

0 = 00 = 000 they are just numbers
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
omar al kababji wrote:
Is 127.0.00.1 a valid IP? I have never seen a double 0 like that.


well open your terminal/cmd and type:

ping 127.0.00.1

0 = 00 = 000 they are just numbers


By that logic, you're implying that 127.0.0000.1 is also valid, since "0000" is just "0".

Also, try this command:



on your PC. Does that mean "127.1" is valid as well? Perhaps the OP should first explain what his/her definition of a valid address is?
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
No its not valid but logically you have the following xxx.xxx.xxx.xxx and thats what we need to check if you exceed the value of 255 then its not valid, if you use more than 3 digits even if the value is less than 255 such as 0023 then its not correct. however i think that if its 000 then its correct.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
omar al kababji wrote:No its not valid but logically you have the following xxx.xxx.xxx.xxx and thats what we need to check if you exceed the value of 255 then its not valid, if you use more than 3 digits even if the value is less than 255 such as 0023 then its not correct. however i think that if its 000 then its correct.


Like I said: the OP would first have to define what valid addresses (may) look like. There's no point in guessing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IP address regular expression