DEV Community

Şammas Çölkesen
Şammas Çölkesen

Posted on

Check IP with Regex in Java

Let's check ip addresses in Java with regex

  • First of all we need to import 2 library.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Enter fullscreen mode Exit fullscreen mode
  • Now, lets define ip regex

      public static final String IP_REGEX ="^"
            + "([01]?\\d\\d?|2[0-4]\\d|22[0-3])\\."
            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
            + "([01]?\\d\\d?|2[0-4]\\d|25[0-4])$";
Enter fullscreen mode Exit fullscreen mode

^ -->start of the line

( --> start of group #1

[01]?\d\d? --> Can be one or two digits. If three digits appear, it must start either 0 or 1
# e.g ([0-9], [0-9][0-9],[0-1][0-9][0-9])

| --> or

2[0-4]\d # start with 2, follow by 0-4 and end with any digit (2[0-4][0-9])

| --> or

25[0-5] # start with 2, follow by 5 and ends with 0-5 (25[0-5])

) --> end of group #2

. --> follow by a dot "."

.... --> repeat with 3 times (3x)

$ -->end of the line

  • Create an pattern object and pass regex string to it.

                String ipregex = System.console().readLine(); //read input

                Pattern ipPattern= Pattern.compile(IP_REGEX); 
                Matcher ipMatcher = ipPattern.matcher(ipregex); 
Enter fullscreen mode Exit fullscreen mode
  • Finally we can control our result.
        //Control flow
        if (!ipMatcher.matches()) { 
                     System.out.println("Yay ! ");
                     }
                else{
                    System.out.println("Noooooo! ");
                }


            return ipregex;

Enter fullscreen mode Exit fullscreen mode

Top comments (0)