Skip to content

Instantly share code, notes, and snippets.

@S4WA
Last active May 20, 2020 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save S4WA/e61ec23a83ff132ce8a142e8e9128812 to your computer and use it in GitHub Desktop.
Save S4WA/e61ec23a83ff132ce8a142e8e9128812 to your computer and use it in GitHub Desktop.
Make parse UUID from no hyphen string (Error: "java.lang.illegalargumentexception:java.lang.illegalargumentexception: Invalid UUID string")
public static UUID parseUUID(String uuid) {
try {
return UUID.fromString(uuid);
} catch (IllegalArgumentException exception) {
return UUID.fromString(uuid.replaceFirst("(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment