Validate IP addresses with Java regular expressions

title: Validating IP Addresses with Java Regular Expressions tags: tech, java —

In Java, you can use regular expressions to validate IP addresses. Regular expressions provide a powerful and flexible way to match patterns in strings. Here, we will see how to use regular expressions to validate if a given string is a valid IP address.

Regular Expression for IP Address Validation

There are different formats for IP addresses, such as IPv4 and IPv6. For the purpose of this example, we will focus on validating IPv4 addresses.

An IPv4 address consists of four numbers separated by periods, ranging from 0 to 255. To validate an IP address, we can use the following regular expression pattern:

String ipPattern = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";

Validating IP Addresses in Java

To validate an IP address in Java, you can use the Pattern and Matcher classes from the java.util.regex package. Here’s an example code snippet that demonstrates how to validate an IP address using regular expressions:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IPAddressValidator {
    public static boolean isValidIPAddress(String ipAddress) {
        // Regular expression pattern for IP address validation
        String ipPattern = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";

        Pattern pattern = Pattern.compile(ipPattern);
        Matcher matcher = pattern.matcher(ipAddress);

        return matcher.matches();
    }

    public static void main(String[] args) {
        String ipAddress1 = "192.168.0.1";
        String ipAddress2 = "256.0.0.1";
        
        System.out.println(ipAddress1 + " is valid: " + isValidIPAddress(ipAddress1));
        System.out.println(ipAddress2 + " is valid: " + isValidIPAddress(ipAddress2));
    }
}

The isValidIPAddress method takes an IP address as input and matches it against the regular expression pattern. If the IP address matches the pattern, it returns true, indicating that the IP address is valid.

In the example main method, we test the validation with two IP addresses - “192.168.0.1” and “256.0.0.1”. The first IP address is valid, while the second one is not.

Conclusion

Regular expressions are an effective tool for validating IP addresses in Java. By using the provided regular expression pattern and the Pattern and Matcher classes, you can easily validate if a given string is a valid IPv4 address.