3

I have already found a question for UUID regular expressions here, but these expressions do not account missing delimeters.

I have come up with the following expression, but is there a more optimal RegEx?

/\b([0-9a-f]{8}-?([0-9a-f]{4}-?){3}[0-9a-f]{12})\b/i

Community
  • 1
  • 1
Mr. Polywhirl
  • 31,606
  • 11
  • 65
  • 114

1 Answers1

0

I am assuming by optimal that you mean a shorter expression. I simplified your regex down to the following:

/[\da-f]{8}-?([\da-f]{4}-?){3}[\da-f]{12}/i, which you can see in action here.

I removed the outer parentheses and \b because everything was correctly matched without them. I was also able to shave three characters off by replacing [0-9a-f] with [\da-f].

I originally had [0-F], but after examining the ASCII sequence, I realized that matched 0123456789:;<=>?@ABCDEF, which includes some extra symbols that we do not want to match.

In conclusion, my expression is synonymous with yours but contains nine fewer characters.

Westy92
  • 11,877
  • 2
  • 53
  • 44