Help, on regex.

How to restrict IP between 127.0.0.0 and 127.255.255.255 by regex?I have tried ip.matches("127\\.[0-255]\\.[0-255]\\.[0-255]"),but it's not work.
[192 byte] By [wushi777.admina] at [2007-10-2 22:09:47]
# 1
Deleted - I posted rubbish!Message was edited by: sabre150
sabre150a at 2007-7-14 1:26:35 > top of Java-index,Java Essentials,Java Programming...
# 2

The [a-b] range thing is for ranges of characters, not ranges of numbers. To do this in regex it would be something like this. (Note, this is not complete or correct, it just gives you the idea of what you'd have to do.)

"127((\\.((0)|(\\d{2})|(1(\\d{2}))|(2[0-4]\\d)|(25[0-5))){3}"

Basically it's "zero OR (any 2-digit number) OR (1 followed by any 2 digits) OR (2 followed by 0-4 followed by any digit) OR (2 followed by 5 followed by 0-5)" -- the whole thing repeated 3 times.

That's just off the top of my head. You'll have to work out the kinks if you want to use it.

Personally, I wouldn't use regex for this. I'd construct a java.net.InetAddres and then see if the first part is "127". That seems simpler to write, simpler to read, and a more direct expression of your intent.

jverda at 2007-7-14 1:26:35 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanks for all, I have know what I shall to do.
wushi777.admina at 2007-7-14 1:26:35 > top of Java-index,Java Essentials,Java Programming...
# 4

I totally forgot one-digit numbers. Don't know what I was thinking with "zero OR 2-digits..." Should have just been "any one digit or 2-digit number" which would be \\d{1,2}, and then special handling for the 3-digit numbers.

But then, like I said, I probably wouldn't use regex for this anyway. :-)

jverda at 2007-7-14 1:26:35 > top of Java-index,Java Essentials,Java Programming...
# 5

Ok, Thank you.

I get a way to do it.

public static void main(String[] args)

{

String ip = "127.0.0.0";

String[] d = ip.split("\\.");

if(ip.matches("127.\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}") && Integer.parseInt(d[1]) <=255 && Integer.parseInt(d[2]) <=255 && Integer.parseInt(d[3]) <= 255)

}

wushi777.admina at 2007-7-14 1:26:35 > top of Java-index,Java Essentials,Java Programming...