Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for UUIDs in text with regex

Tags:

regex

I'm searching for UUIDs in blocks of text using a regex. Currently I'm relying on the assumption that all UUIDs will follow a patttern of 8-4-4-4-12 hexadecimal digits.

Can anyone think of a use case where this assumption would be invalid and would cause me to miss some UUIDs?

like image 996
Guy Avatar asked Sep 25 '08 22:09

Guy


People also ask

How do I find the UUID of a string?

So to check if a string is valid UUID we can use this regular expression, // Regular expression to check if string is a valid UUID const regexExp = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi; This matches all the valid UUID since it checks for the above test cases.

How do I find a character in a string in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What is UUID regex?

A UUID is a 128-bit number represented, textually, in 16 octets as 32 hexadecimal (base-16) digits. These 32 digits are displayed in 5 groups separated by hyphens, in the form 8-4-4-4-12, for a total of 36 characters. You may edit the regex to your liking for removing hyphens.

What is UUID text?

A UUID – that's short for Universally Unique IDentifier, by the way – is a 36-character alphanumeric string that can be used to identify information (such as a table row).


2 Answers

The regex for uuid is:

\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b 
like image 159
Ivelin Avatar answered Sep 19 '22 21:09

Ivelin


@ivelin: UUID can have capitals. So you'll either need to toLowerCase() the string or use:

[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}

Would have just commented this but not enough rep :)

like image 28
Matthew F. Robben Avatar answered Sep 19 '22 21:09

Matthew F. Robben