• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IP address regular expression

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would be easiest, not requiring any RE
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from a simple google search:


 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried it with 127.0.0.1, 127..0.1 and 127.00.0.1?
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it works. did you tried it ??
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is 127.0.00.1 a valid IP? I have never seen a double 0 like that.
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic