How to test valid UUID/GUID?
Currently, UUID's are as specified in RFC4122. An often neglected edge case is the NIL UUID, noted here. The following regex takes this into account and will return a match for a NIL UUID. See below for a UUID which only accepts non-NIL UUIDs. Both of these solutions are for versions 1 to 5 (see the first character of the third block).
Therefore to validate a UUID...
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i
...ensures you have a canonically formatted UUID that is Version 1 through 5 and is the appropriate Variant as per RFC4122.
NOTE: Braces {
and }
are not canonical. They are an artifact of some systems and usages.
Easy to modify the above regex to meet the requirements of the original question.
HINT: regex group/captures
To avoid matching NIL UUID:
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
How to check UUID validity in Python?
I found this question while I was looking for a Python answer. To help people in the same situation, I've added the Python solution.
You can use the uuid
module:
#!/usr/bin/env python
from uuid import UUID
def is_valid_uuid(uuid_to_test, version=4):
"""
Check if uuid_to_test is a valid UUID.
Parameters
----------
uuid_to_test : str
version : {1, 2, 3, 4}
Returns
-------
`True` if uuid_to_test is a valid UUID, otherwise `False`.
Examples
--------
>>> is_valid_uuid('c9bf9e57-1685-4c89-bafb-ff5af830be8a')
True
>>> is_valid_uuid('c9bf9e58')
False
"""
try:
uuid_obj = UUID(uuid_to_test, version=version)
except ValueError:
return False
return str(uuid_obj) == uuid_to_test
if __name__ == '__main__':
import doctest
doctest.testmod()
How to check the validity of a GUID (or UUID) using NSRegularExpression or any other effective way in Objective-C
This regex matches for me
\A\{[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}\}\Z
In short:
\A
and\Z
is the beginning and end of the string\{
and\}
is escaped curly bracets[A-F0-9]{8}
is exactly 8 characters of either 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
As an NSRegularExpression it would look like this
NSError *error = NULL;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"\\A\\{[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}\\}\\Z"
options:NSRegularExpressionAnchorsMatchLines
error:&error];
// use the regex to match the string ...
How to validate UUID v4 in Go?
Try with...
func IsValidUUID(uuid string) bool {
r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
return r.MatchString(uuid)
}
Live example: https://play.golang.org/p/a4Z-Jn4EvG
Note: as others have said, validating UUIDs with regular expressions can be slow. Consider other options too if you need better performance.
How to check UUID validity in Python?
I found this question while I was looking for a Python answer. To help people in the same situation, I've added the Python solution.
You can use the uuid
module:
#!/usr/bin/env python
from uuid import UUID
def is_valid_uuid(uuid_to_test, version=4):
"""
Check if uuid_to_test is a valid UUID.
Parameters
----------
uuid_to_test : str
version : {1, 2, 3, 4}
Returns
-------
`True` if uuid_to_test is a valid UUID, otherwise `False`.
Examples
--------
>>> is_valid_uuid('c9bf9e57-1685-4c89-bafb-ff5af830be8a')
True
>>> is_valid_uuid('c9bf9e58')
False
"""
try:
uuid_obj = UUID(uuid_to_test, version=version)
except ValueError:
return False
return str(uuid_obj) == uuid_to_test
if __name__ == '__main__':
import doctest
doctest.testmod()
PHP verify valid UUID
According to Wikipedia the format of UUID v4 is:
Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
where x is any hexadecimal digit and y is one of 8, 9, A, or B. e.g.
f47ac10b-58cc-4372-a567-0e02b2c3d479
.
The corresponding regex is:
/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
How to judge a string is UUID type?
You should use regex to verify it e.g.:
^[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}$
test it with e.g.g 01234567-9ABC-DEF0-1234-56789ABCDEF0
or with brackets
^\{?[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}\}?$
Related Topics
What's the Best Way to Loop Through a Set of Elements in JavaScript
JavaScript Accessing Inner Dom of Svg
How to Check If a Number Is Between Two Values
How to Trigger a Link's (Or Any Element'S) Click Event Through JavaScript
How to Parse a Url Query Parameters, in JavaScript
Deep Copy in Es6 Using the Spread Syntax
A Proper Wrapper for Console.Log with Correct Line Number
How to Set the Prototype of a JavaScript Object That Has Already Been Instantiated
Convert Time Interval Given in Seconds into More Human Readable Form
How to Download File from Server
How to Get All Options of a Select Using Jquery
Onclick Event Not Firing on Jsfiddle.Net
Firefox Error Rendering an Svg Image to HTML5 Canvas with Drawimage
Chrome Extension: Port Error: Could Not Establish Connection. Receiving End Does Not Exist
Typescript Type 'String' Is Not Assignable to Type
Typeerror: _Firebase_Webpack_Imported_Module_2_.Default.Collection Is Not a Function