Search code examples
javaregexvalidationuuid

Validate UUID without divider "-"


I get UUIDs in the format like "005056963AB75FD48BDC59C100314C40" and want to validate them. I tried code like this:

public boolean isUUID(String uuid){

    try {
        UUID.fromString(uuid);
    } catch (Exception e) {
        return false;
    }
    return true;

}

But this will tell me that "005056963AB75FD48BDC59C100314C40" is not a valid ID. The site http://guid.us/Test/GUID on the other hand tells me it is and gives me back an UUID with the "-" added. Is there an elegant way to validate this UUID in java or do I have to manually add "-" to the right places?


Solution

  • try regex

        uuid = uuid.replaceAll("(.{8})(.{4})(.{4})(.{4})(.+)", "$1-$2-$3-$4-$5");
        UUID.fromString(uuid);