Validate UUID

In this article, we will take a look at 2 different ways to validate a UUID string in java with examples.

What is UUID ?

UUID stands for Universal Unique IDentifier.
A UUID is a unique identifier that is used to identify a device or a piece of data. UUIDs are typically generated by a device or a service, and are then used to identify that device or service.

UUID has a fixed format having 5 blocks where each block has a fixed length of digits as below

NameLength(Hex digits)
time_low8
time_mid4
time_high_and_version4
variant_and_sequence4
node12

Remember that UUID comprises of alphanumeric characters.

Reasons to validate

There are a couple of reasons to validate a UUID string in Java.
1. To make sure that a UUID string that is being passed to your Java application is in the correct format.
2. To validate a UUID string to make sure that it is not being used to spoof a device or service.

Validate UUID string in Java

There are 2 ways to validate a UUID string in Java.

1. Using java.util.UUID

This class has a static method fromString() that takes a string argument returns an object of java.util.UUID.
If the string is not in the correct format, then this method will throw an IllegalArgumentException. Example,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
UUID valid = UUID.fromString("30de749c-10b8-4be0-a077-5eb486197a29");
System.out.println(valid);
UUID invalid= UUID.fromString("AF");
System.out.println(invalid);
UUID valid = UUID.fromString("30de749c-10b8-4be0-a077-5eb486197a29"); System.out.println(valid); UUID invalid= UUID.fromString("AF"); System.out.println(invalid);
UUID valid = UUID.fromString("30de749c-10b8-4be0-a077-5eb486197a29");
System.out.println(valid);
UUID invalid= UUID.fromString("AF");
System.out.println(invalid);

Output is

30de749c-10b8-4be0-a077-5eb486197a29
Exception in thread “main” java.lang.IllegalArgumentException: Invalid UUID string: AF
at java.base/java.util.UUID.fromString1(UUID.java:280)
at java.base/java.util.UUID.fromString(UUID.java:258)
at com.codippa.UUIDValidator.main(UUIDValidator.java:34)

2. Using regex

Another way to validate a UUID string is to use a regular expression.
A regular expression is a pattern that can be used to match a string. There are a few different regular expressions that can be used to match a UUID string.
One of those is

[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}

This regular expression will match a UUID string that is in the correct format.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String regex = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
Pattern pattern = Pattern.compile(regex);
System.out.println(
pattern.matcher("c0d69b05-c468-4d28-9779-c433e7364be6").matches());
System.out.println(
pattern.matcher("ABFCD").matches());
String regex = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"; Pattern pattern = Pattern.compile(regex); System.out.println( pattern.matcher("c0d69b05-c468-4d28-9779-c433e7364be6").matches()); System.out.println( pattern.matcher("ABFCD").matches());
String regex = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
Pattern pattern = Pattern.compile(regex);
System.out.println(
      pattern.matcher("c0d69b05-c468-4d28-9779-c433e7364be6").matches());
System.out.println(
      pattern.matcher("ABFCD").matches());

Output is

true
false

Conclusion

This article demonstrated 2 different ways to validate UUID string in java.
First is using UUID class in java itself.
Second is to validate the UUID string against a regular expression based on the format of standard UUID.

Categorized in:

Java String,