Created
June 1, 2012 16:32
-
-
Save thbounzer/2853387 to your computer and use it in GitHub Desktop.
Reminder code for regexp parsing in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final Pattern IP_PATTERN = Pattern.compile("\\A(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})\z"); | |
private boolean ipAddressValidator(String iPaddress){ | |
Matcher m = IP_PATTERN.matcher(iPaddress); | |
boolean matches = m.matches(); | |
boolean validIp = false; | |
int[] octects = extractIpOctets(iPaddress); | |
for(int octet: octects){ | |
if (octet > 255){ | |
validIp = false; | |
break; | |
} else { | |
validIp = true; | |
} | |
} | |
return matches && validIp; | |
} | |
private int[] extractIpOctets(String ip){ | |
int[] octects = new int[4]; | |
Matcher group = IP_PATTERN.matcher(ip); | |
boolean matchFound = group.find(); | |
if (matchFound) { | |
for (int i=1; i<=group.groupCount(); i++) { | |
octects[i-1] = Integer.valueOf(group.group(i)); | |
} | |
} | |
return octects; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment