Created
January 28, 2018 06:39
-
-
Save seLain/43916f633fb6dde12eca7329dc1687a1 to your computer and use it in GitHub Desktop.
regular expression for IPv4 validation (demo with Java code)
This file contains 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
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