
UUID regex Java
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:
Pattern.compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$")
Enter a text in the input above to see the result
Example code in Java:
import java.util.regex.Pattern;
import java.util.regex.MatchResult;
import java.util.Arrays;
public class Main {
public static void main(String []args) {
// Validate UUID
boolean isMatch = Pattern.compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$")
.matcher("123e4567-e89b-12d3-a456-426614174000")
.find();
System.out.println(isMatch); // prints true
// Extract UUID from a string
String[] matches = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}")
.matcher("The UUID 123e4567-e89b-12d3-a456-426614174000 for node Node 01 is not unique.")
.results()
.map(MatchResult::group)
.toArray(String[]::new);
System.out.println(Arrays.toString(matches)); // prints [123e4567-e89b-12d3-a456-426614174000]
}
}