1

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?

jan
  • 3,923
  • 9
  • 38
  • 78

4 Answers4

5

try regex

    uuid = uuid.replaceAll("(.{8})(.{4})(.{4})(.{4})(.+)", "$1-$2-$3-$4-$5");
    UUID.fromString(uuid);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

You can use this Java regex code to put hyphens at right place in UUID string:

String s = "005056963AB75FD48BDC59C100314C40";
s = s.replaceAll(
        "(?i)([a-f0-9]{8})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{12})", 
        "$1-$2-$3-$4-$5");

System.out.printf("UUID: [%s]%n", s);
//=> 00505696-3AB7-5FD4-8BDC-59C100314C40

Update: Non regex solution (thnks to @atamanroman)

StringBuilder sbr = new StringBuilder(s);
for(int i=8, j=0; i<=20; i+=4, j++)
    sbr.insert(i+j, '-');

UUID uuid = UUID.fromString( sbr.toString() );
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

To add dashes to an UUID string, I would suggest to use StringBuilder#insert(int offset, chat c) as @atamanroman said.

final String uuid = new StringBuilder("005056963AB75FD48BDC59C100314C40")
        .insert(20, '-')
        .insert(16, '-')
        .insert(12, '-')
        .insert(8, '-')
        .toString();

System.out.println(uuid);
GabyTM
  • 1
  • 1
  • 3
0

This is an example of function that validates UUID strings without hyphens. It's faster because it doesn't use regexp or buffering. A UUID without hyphens is just a string of 32 hexadecimal chars.

public class Example1 {
    /**
     * Validate UUID without hyphens.
     * 
     * @param uuid a uuid string
     * @return true if valid
     */
    public static boolean isValid(String uuid) {

        if (uuid == null || uuid.length() != 32) {
            return false;
        }

        final char[] chars = uuid.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            final int c = chars[i];
            if (!((c >= 0x30 && c <= 0x39) || (c >= 0x61 && c <= 0x66) || (c >= 0x41 && c <= 0x46))) {
                // ASCII codes: 0-9, a-f, A-F
                return false;
            }
        }
        
        return true;
    }

    public static void main(String[] args) {
        String uuid = "005056963AB75FD48BDC59C100314C40";
        if(isValid(uuid)) {
            // do something
        }
    }
}

You can also use UuidValidator.isValid() from the library uuid-creator. It validates UUID strings with or without hyphens. This is another example:

import com.github.f4b6a3.uuid.util.UuidValidator;

public class Example2 {

    public static void main(String[] args) {
        String uuid = "005056963AB75FD48BDC59C100314C40";
        if(UuidValidator.isValid(uuid)) {
            // do something
        }
    }
}

But if you just want to convert a string into a UUID, you can use UuidCreator.fromString(). This function also validates the input.

import java.util.UUID;
import com.github.f4b6a3.uuid.util.UuidValidator;

public class Example3 {
    public static void main(String[] args) {
        String uuidString = "005056963AB75FD48BDC59C100314C40";
        UUID uuid = UuidCreator.fromString(uuidString);
    }
}

Project page: https://github.com/f4b6a3/uuid-creator

fabiolimace
  • 972
  • 11
  • 13