Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this tutorial, we're going to have a look at some ways of validating UUID (Universally Unique Identifier) strings in Java.

We'll go through one of the UUID class methods, and then we'll use regular expressions.

2. Using UUID.fromString()

One of the quickest ways of checking if a String is a UUID is by trying to map it using the static method fromString belonging to the UUID class. Let's try it out:

@Test
public void whenValidUUIDStringIsValidated_thenValidationSucceeds() {
    String validUUID = "26929514-237c-11ed-861d-0242ac120002";
    Assertions.assertEquals(UUID.fromString(validUUID).toString(), validUUID);

    String invalidUUID = "invalid-uuid";
    Assertions.assertThrows(IllegalArgumentException.class, () -> UUID.fromString(invalidUUID));
}

In the above code snippet, we can see that in case the string we're trying to validate doesn't represent a UUID, then an IllegalArgumentException will be thrown. However, the method fromString will return `00000001-0001-0001-0001-000000000001` for strings such as `1-1-1-1-1`. So we have included the string comparison to take care of this case.

Some might argue that using exceptions is not a good practice for flow control, so we're going to see a different way of achieving the same result.

3. Using Regular Expressions

Another way of validating a UUID is to use a regular expression that will match exactly the format.

Firstly, we need to define a Pattern that will be used to match the string.

Pattern UUID_REGEX =
  Pattern.compile("^[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}$");

Then, we can use this pattern to try matching it to a string in order to validate whether or not it is a UUID:

@Test
public void whenUUIDIsValidatedUsingRegex_thenValidationSucceeds() {
    Pattern UUID_REGEX =
      Pattern.compile("^[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}$");

    Assertions.assertTrue(UUID_REGEX.matcher("26929514-237c-11ed-861d-0242ac120002").matches());

    Assertions.assertFalse(UUID_REGEX.matcher("invalid-uuid").matches());
}

4. Conclusion

In this article, we've learned how to validate a UUID string by using regular expressions or by taking advantage of the static method of the UUID class.

As always, the code for these examples is available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!