Skip to content

Instantly share code, notes, and snippets.

@thbounzer
Created June 1, 2012 16:32
Show Gist options
  • Save thbounzer/2853387 to your computer and use it in GitHub Desktop.
Save thbounzer/2853387 to your computer and use it in GitHub Desktop.
Reminder code for regexp parsing in java
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