REGEXVAULT
Identity & PII/Digital Identity
ReDoS Checked

IPv4 Address (as PII identifier) Regex for Java

/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$/

All five implementations are free. Switch engines without signing in.

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching ipv4 address (as pii identifier), ported and verified for Java. Identity and credential patterns need both correctness and safety, since they're frequent targets for adversarial input. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.

Java Implementation

Java
// IPv4 Address (as PII identifier)
// ReDoS-checked | RegexVault — Identity & PII > Digital Identity

import java.util.regex.Pattern;

public class Ipv4AddressAsPiiIdentifierValidator {
    private static final Pattern PATTERN =
        Pattern.compile("^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$");

    public static boolean validate(String input) {
        return PATTERN.matcher(input).matches();
    }

    // Example
    public static void main(String[] args) {
        System.out.println(validate("192.168.1.1")); // true
    }
}

Test Cases

Matches (Valid)
Rejects (Invalid)
192.168.1.1192.168.1.256
10.0.0.1192.168.1
8.8.8.8192.168.1.1.1
255.255.255.255192.168.01.1
0.0.0.0

When to use this pattern

This pattern is drawn from the Identity & PII > Digital Identity category and has been checked for ReDoS risk. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

Anonymizing IPs by zeroing the last octet (192.168.1.0 from 192.168.1.50) is a common technique for analytics. GDPR Article 25 (Privacy by Design) recommends this for non-essential logging.

Technical Notes

Under GDPR (C-582/14, Breyer v Germany), IP addresses can constitute personal data when the controller has the legal means to identify the individual. Dynamic IPs assigned by ISPs are personal data. Log files containing IP addresses must comply with GDPR retention requirements.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Submissions join the review queue after automated validation.

Submit a Pattern