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?
- 2Try stackoverflow.com/questions/18724750/… - it might be just what you are looking for.Ewald– Ewald2013-11-18 05:58:12 +00:00Commented Nov 18, 2013 at 5:58
- 2Possible duplcate of: stackoverflow.com/questions/7905929/how-to-test-valid-uuid-guidMasudul– Masudul2013-11-18 05:59:00 +00:00Commented Nov 18, 2013 at 5:59
- I added a UUID validator to Apache Commons Validator. It's not yet been merged, but you can vote for it here: github.com/apache/commons-validator/pull/68Daniel Heid– Daniel Heid2022-01-07 09:18:06 +00:00Commented Jan 7, 2022 at 9:18
Add a comment |
2 Answers
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 } 10 Comments
erickson
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.
erickson
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.
mkilic
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();ampofila
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)"
Josh M.
@mkilic Sounds like a bug in the JDK. Also sucks we don't have
UUID.tryParse(String value) or similar. |
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
Victor
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); }MartinPicker
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
Josh M.
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.
TomPlum
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}$)
|