PHP preg_match UUID v4
Asked Answered
A

4

15

I've got a string that contains UUID v4

$uuid = 'http://domain.com/images/123/b85066fc-248f-4ea9-b13d-0858dbf4efc1_small.jpg';

How would i get the b85066fc-248f-4ea9-b13d-0858dbf4efc1 value from the above using preg_match()?
More info on UUID v4 can be be found here

Anthozoan answered 3/6, 2011 at 5:8 Comment(2)
not sure but try "^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"Glade
Again, Rufinus, this is wrong, see en.wikipedia.org/wiki/…Rosmunda
C
22
$uuid = 'http://domain.com/images/123/b85066fc-248f-4ea9-b13d-0858dbf4efc1_small.jpg';
preg_match('!/images/\d+/([a-z0-9\-]*)_!i', $uuid, $m);

And

preg_match('/[a-f0-9]{8}\-[a-f0-9]{4}\-4[a-f0-9]{3}\-(8|9|a|b)[a-f0-9]{3‌​}\-[a-f0-9]{12}/', $uuid, $m);

works too. Taken from here, but I don't know if we can rely on that.

Clevis answered 3/6, 2011 at 5:12 Comment(5)
No, this regex is waay too permissive.Rosmunda
The question is how to get the uuid from link like that but not from anywhere, e.g. not from text. UUID Definition is here, so I guess my second solution which is, actually, stolen, would work right for this purpose.Clevis
A more strict regex would be /[a-f0-9]{8}\-[a-f0-9]{4}\-4[a-f0-9]{3}\-(8|9|a|b)[a-f0-9]{3}\-[a-f0-9]{12}/Baud
If the regex doesn't work though it looks correct: there are some invisible characters in the code examples which show up in the html source: ‌​ Working version without code highlighting: /[a-f0-9]{8}\-[a-f0-9]{4}\-4[a-f0-9]{3}\-(8|9|a|b)[a-f0-9]{3}\-[a-f0-9]{12}/Subtype
@ZachMertes The RFC also allows uppercase lettersSimmie
E
8

You can try this simple pattern for uuids

preg_match('/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/',$uuid,$matches);
Elvia answered 3/6, 2011 at 5:18 Comment(2)
This is matching ________-____-____-____-____________Gabrila
Yeah, so don't use this. At least replace \w with [0-9a-f]: '/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/'Pavis
T
0

The accepted answer has several flaws:

  • It includes hidden characters that break the regex
  • It does not allow for uppercase letters (some or all)
  • It does not allow for optional dashes

See Wikipedia for details on allowed representations of a v4 UUID.

A regex check that meets the requirements above is:

preg_match(/(?i)[a-f0-9]{8}[\-]?[a-f0-9]{4}[\-]?4[a-f0-9]{3}[\-]?(8|9|a|b)[a-f0-9]{3}[\-]?[a-f0-9]{12}/, $uuid, $matches);

This option will meet most cases, however a more thorough version would address the following flaws:

  • It doesn't allow for the nil UUID (all 0) or the max UUID (all F)
  • It doesn't allow for the Windows-based format that includes leading and trailing braces ({...})

A regex check that meets these requirements as well is:

preg_match(/(?i){?[a-f0-9]{8}[\-]?[a-f0-9]{4}[\-]?(4[a-f0-9]{3}|[0]{4}|[F]{4})[\-]?((8|9|a|b)[a-f0-9]{3}|[0]{4}|[F]{4})[\-]?[a-f0-9]{12}}?/, $uuid, $matches);

These pieces mean:

  • (?i): force case insensitivity
  • {?: 0 or 1 opening braces
  • [a-f0-9]{8}: 8 allowed characters
  • [\-]?: 0 or 1 dashes
  • [a-f0-9]{4}: 4 allowed characters
  • [\-]?: 0 or 1 dashes
  • (4[a-f0-9]{3}|[0]{4}|[F]{4}): a 4 followed by 3 allowed characters, or 4 zeroes, or 4 Fs
  • [\-]?: 0 or 1 dashes
  • ((8|9|a|b)[a-f0-9]{3}|[0]{4}|[F]{4}): a 4 followed by 3 allowed characters, or 4 zeroes, or 4 Fs
  • [\-]?: 0 or 1 dashes
  • [a-f0-9]{12}: 12 allowed characters
  • }?: 0 or 1 closing braces

For any of you regex wizards out there, this could further be improved if it's possible to do the following:

  • Require that if one dash is present, all four dashes must be present
  • Only allow all zeroes or all Fs in some blocks if the entire UUID is all zeroes or Fs
  • Only allow an opening or closing brace if both are present
Talanta answered 5/3, 2024 at 14:40 Comment(0)
M
-3

When testing if string is matching, then don't forget ^ on beginning and $ on ending in pattern. Without them you can test, if string contains requested string, but not that it match requested string. '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/'

Molecular answered 18/9, 2023 at 11:25 Comment(1)
The question is seeking to extract the UUID from the string, the use of ^ and $ around the pattern for the UUID would not match a string that contained the full URLUrceolate

© 2022 - 2025 — McMap. All rights reserved.