tags:

views:

71

answers:

2

Consider a regex for testing port numbers.

(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3})

This is not valid in Android.

Any idea what a port number regex should look like in Android?

A: 

"(6553[0-5]|655[0-2]\\d|65[0-4]\\d{2}|6[0-4]\\d{3}|[1-5]\\d{4}|[1-9]\\d{0,3})" works on a Java Regex test page assuming that you are writing Java code. You probably have to escape the backslashes for the Java string literal to work. However, this expression does not thing that zero is a valid port number.

D.Shawley
+4  A: 

Generally, regex isn't that great for numerical validation. I'd recommend using Integer.parseInt on a matched group and then check that to see if it's less than 65536.

Quartz
Agreed. I tried to validate an integer range for a tokenizer with regex once and got super frustrated trying to get it right. Match [0-9]+ and then get it into a number format.
colithium