I'm attempting to check if a string contains only
1
0
.
or a combination of these three.
First I had this code:
public static boolean controleSubnetmask(String mask) {
try {
String[] maskArray = mask.split(".");
int[] subnetmask = new int[4];
//array of string to array of int
for (int i = 0; i < maskArray.length; i++) {
subnetmask[i] = Integer.parseInt(maskArray[i]);
}
return true;
} catch (NumberFormatException e) {
return false;
}
}
but that is rather complicated for what it does, and it doesn't check if only 1 and 0 are entered. So now I have this, but it seems I misunderstood regular expressions because it doesn't work:
public static void controleSubnetmask(String mask) {
mask = "1100.110...11";
String test = "p";
if (mask.contains("[^10\\.]") == true) {
System.out.println("wrong input");
}
if (test.contains("[^10\\.]") == true) {
System.out.println("wrong input");
}
}
I expected a 'wrong input' message on the test String, which didn't appear. So I believe my regex:
[^01\\.]
is wrong, but I really have no clue how to specify it. Thanks in advance!