1

I am attempting to validate UUID input from a user. I've tried the suggestions from previous similar questions here...

private static final Pattern pattern = Pattern
.compile("(?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-5][0-9a-f]{3}-?[089ab][0-9a-f]{3}-?[0-9a-f]{12}$");

private static final Pattern pattern = Pattern
.compile("/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i");

private static final boolean isValidUuid(final String uuid) {
        return ((uuid != null) && (uuid.trim().length() > 31)) ? pattern.matcher(uuid).matches() : false;
      }

public boolean isUUID(String s){
        return s.matches("/^[0-9a-f]{8}-?[0-9a-f]{4}-?[1-5][0-9a-f]{3}-?[89ab][0-9a-f]{3}-?[0-9a-f]{12}$/i");
    }

UUID id = UUID.toString(inputUUID) 
id.toString().equals(inputUuid)

None of these successfully validate user input of "00000000000000000000000000000001"

Appreciate any suggestions!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kelly
  • 11
  • 3
  • The UUID 00000000000000000000000000000001 validates at http://guid.us/Test/GUID – Kelly Apr 28 '19 at 02:06
  • Why are there 2 different definitions for `pattern`? And there seems to be some missing code at the end at `UUID.toString`? – Gino Mempin Apr 28 '19 at 02:22
  • A couple of the answers on SO showed different patterns...for the types of UUIDs I believe. Neither work to validate the input – Kelly Apr 28 '19 at 02:24
  • And yes...there is missing code...id = UUID.toString(inputUUID) id.toString.equals(inputUUID)...I was just indicating that I tried the various suggested approaches but none validate the uuid. – Kelly Apr 28 '19 at 02:25
  • Please use the **[edit]** link to update your post with missing/additional info. – Gino Mempin Apr 28 '19 at 02:26
  • 1
    Throw it away. Just try to construct a `java.util.UUID` from it. It succeeds, it's OK, otherwise it isn't. – user207421 Apr 28 '19 at 03:40
  • 1
    `00000000000000000000000000000001` is not a valid UUID... – Mark Rotteveel Apr 28 '19 at 08:54
  • Mark...it validates at guid.us...not sure why you would say it is not a valid UUID. – Kelly Apr 28 '19 at 18:13

0 Answers0