Skip to content

Latest commit

 

History

History
22 lines (15 loc) · 537 Bytes

validate-a-uuid-string-in-java.md

File metadata and controls

22 lines (15 loc) · 537 Bytes

Validate A UUID String In Java

Category: Java

You can validate a UUID string is in the correct format with the following Java code.

import java.util.regex.Pattern;

public class UUIDValidator {

    private final static Pattern UUID_REGEX_PATTERN = Pattern.compile("^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$");

    public static boolean isValidUUID(String uuid) {
        if (uuid == null) {
            return false;
        }
        return UUID_REGEX_PATTERN.matcher(uuid).matches();
    }

}