Skip to content

Instantly share code, notes, and snippets.

@seLain
Created January 28, 2018 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seLain/43916f633fb6dde12eca7329dc1687a1 to your computer and use it in GitHub Desktop.
Save seLain/43916f633fb6dde12eca7329dc1687a1 to your computer and use it in GitHub Desktop.
regular expression for IPv4 validation (demo with Java code)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
class Solution{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String IP = in.next();
System.out.println(IP.matches(new MyRegex().pattern));
}
}
}
class MyRegex {
public String pattern;
public MyRegex(){
String single_number = "([0]{0,2}[0-9]|[0]{0,1}[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])";
this.pattern = "(" + single_number + "\\.){3}" + single_number;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment