UUID regex C#

UUID

UUID is a 128-bit label used for identifications in computer systems.

UUID regex

Below is a simple UUID regular expression that will work most of the time:

new Regex("^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$")

Test it!
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i

True

False

Enter a text in the input above to see the result

Example code in C#:

using System.Text.RegularExpressions;
using System.Linq;
using System;
                    
public class Program
{
    public static void Main()
    {
        // Validate UUID
        Regex validateUUIDRegex = new Regex("^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$");
        Console.WriteLine(validateUUIDRegex.IsMatch("123e4567-e89b-12d3-a456-426614174000"));  // prints True
        
        // Extract UUID from a string
        Regex extractUUIDRegex = new Regex("[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}");
        string [] extracted = extractUUIDRegex.Matches("The UUID 123e4567-e89b-12d3-a456-426614174000 for node Node 01 is not unique.")
            .Cast()
            .Select(m => m.Value) 
            .ToArray(); 
        Console.WriteLine(String.Join(",", extracted)); // prints 123e4567-e89b-12d3-a456-426614174000
    }
}

Test it!

True

False

Enter a text in the input above to see the result

Test it!

True

False

Enter a text in the input above to see the result