74

In my project, I use UUID.fromString() to convert string to UUID, but if the string is not UUID type, it will throw exception, so how can I validate this string?

3

2 Answers 2

120

Handle the exception and do something in that case. For example :

try{ UUID uuid = UUID.fromString(someUUID); //do something } catch (IllegalArgumentException exception){ //handle the case where string is not valid UUID } 
Sign up to request clarification or add additional context in comments.

10 Comments

It might be unexceptional to have data that contain UUIDs and other elements. Exceptions should be exceptional, not routine; don't use exceptions for flow control. There are a number of reasons for this.
It's reasonable to handle it there if, in the context of your application, you expect to have a UUID and it would be exceptional for the string to be an invalid format. On the other hand, if your application is a parser, and the grammar accepted UUIDs and identifiers of other types at a particular alternative, testing the format like this would be inappropriate.
This would not work! I run into an issue where the someUUID is "1-1-1-1-1" but UUID.formString("1-1-1-1-1") returned "00000001-0001-0001-0001-000000000001", basically it does convert into an UUID representation. you need to use regex like Pattern p = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"); and then p.matcher(uuidString).matches();
Please note that this does not work for Java 8. See here bugs.java.com/view_bug.do?bug_id=8159339 . As an alternative you can use "UUID.fromString(uuid).toString().equals(uuid)"
@mkilic Sounds like a bug in the JDK. Also sucks we don't have UUID.tryParse(String value) or similar.
|
92

You should use regex to verify it e.g.:

^[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}$ 

test it with e.g.g 01234567-9ABC-DEF0-1234-56789ABCDEF0

or with brackets

^\{?[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}‌​\}?$ 

9 Comments

Just throwing my 2 cents here, as I saw the other answer with 35 up-votes. People should use this one if those are the 2 choices. Mainly because I found that the JDK I was using was simply throwing the exception based on this trivia if -> String[] var1 = var0.split("-"); if (var1.length != 5) { throw new IllegalArgumentException("Invalid UUID string: " + var0); }
The second regular expression above that allows brackets has a problem. It considers a UUID with only an opening or closing curly brace valid, ex: {77921671-41fa-4f61-83ea-5d60b76a264e
This is far more prone to breakage, and far more verbose and unnecessary than a simple try/catch to parse the UUID as in another answer here.
You could condense the redundancy of the {4} length groups by wrapping it in a non-capturing group with a {3} quantifier. E.g. (^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.