如何测试有效的UUID/GUID?
如何检查变量是否包含有效的 UUID/GUID 标识符?
我目前只对验证类型 1 和 4 感兴趣,但这不应该限制您的答案。
How to check if variable contains valid UUID/GUID identifier?
I'm currently interested only in validating types 1 and 4, but it should not be a limitation to your answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
您可以在社区支持下使用 class-validator 。
You can use class-validator with community support..
我认为更好的方法是使用静态方法 fromString 来避免这些正则表达式。
另一方面
抛出 java.lang.IllegalArgumentException: Invalid UUID string: x
I think a better way is using the static method fromString to avoid those regular expressions.
On the other hand
throws java.lang.IllegalArgumentException: Invalid UUID string: x
我认为 Gambol 的答案几乎是完美的,但它误解了 RFC 4122 § 4.1.1.变体部分 一点。
它涵盖 Variant-1 UUID (10xx = 8..b),但不涵盖 Variant-0 (0xxx = 0..7) 和 Variant-2 (110x = c..d) 变体,这些变体是为向后兼容而保留的,因此它们在技术上是有效的 UUID。 Variant-4 (111x = e..f) 确实保留供将来使用,因此它们当前无效。
另外,0 类型无效,如果“数字”是 NIL UUID,则仅允许为 0(如 Evan 的回答<中提到的) /a>)。
所以我认为符合当前 RFC 4122 规范的最准确的正则表达式是(包括连字符):
I think Gambol's answer is almost perfect, but it misinterprets the RFC 4122 § 4.1.1. Variant section a bit.
It covers Variant-1 UUIDs (10xx = 8..b), but does not cover Variant-0 (0xxx = 0..7) and Variant-2 (110x = c..d) variants which are reserved for backward compatibility, so they are technically valid UUIDs. Variant-4 (111x = e..f) is indeed reserved for future use, so they are not valid currently.
Also, 0 type is not valid, that "digit" is only allowed to be 0 if it's a NIL UUID (like mentioned in Evan's answer).
So I think the most accurate regex that complies with current RFC 4122 specification is (including hyphens):
版本 1 到 5,省略版本时不使用多版本正则表达式。
Versions 1 to 5, without using a multi-version regex when version is omitted.
在 Node 中执行此操作的一个好方法是使用
ajv
包 (https:// github.com/epoberezkin/ajv)。A good way to do it in Node is to use the
ajv
package (https://github.com/epoberezkin/ajv).我有这个功能,但它本质上与接受的答案相同。
I have this function, but it's essentially the same as the accepted answer.
使用 .match() 方法检查 String 是否为 UUID。
Use the .match() method to check whether String is UUID.
这是在 JavaScript 中检查字符串是否为有效 UUID 的最简单方法
Here is the easiest way to check if a string is a valid UUID in JavaScript
如果您使用 uuid 包,该包会带来一个布尔验证函数,它会告诉您 uuid 是否有效。
例子:
If you use the uuid package, this package brings a boolean validation function where it tells you if a uuid is valid or not.
Example:
如果您使用 Node.js 进行开发,建议使用名为 Validator 的包。它包括验证不同版本的 UUID 所需的所有正则表达式,此外您还可以获得各种其他验证函数。
这是 npm 链接:验证器
If you are using Node.js for development, it is recommended to use a package called Validator. It includes all the regexes required to validate different versions of UUID's plus you get various other functions for validation.
Here is the npm link: Validator
感谢@usertatha,做了一些修改
thanks to @usertatha with some modification
除了Gambol 的答案几乎在所有情况下都能完成这项工作之外,到目前为止给出的所有答案都没有分组格式 (8-4-4-4-12) 对于 在文本中编码 GUID。它使用得非常频繁,但显然 32 个十六进制数字的普通链也是有效的。[1] regexenh:
[1 ] 问题是关于检查变量,因此我们也应该包括用户不友好的表单。
Beside Gambol's answer that will do the job in nearly all cases, all answers given so far missed that the grouped formatting (8-4-4-4-12) is not mandatory to encode GUIDs in text. It's used extremely often but obviously also a plain chain of 32 hexadecimal digits can be valid.[1] regexenh:
[1] The question is about checking variables, so we should include the user-unfriendly form as well.
到目前为止发布的所有特定于类型的正则表达式都在“类型 0”Nil UUID 上失败,该 UUID 在 RFC 4.1.7 中定义为:
要修改 Wolf 的答案:
或者,正确排除“类型 0” “如果没有全零,我们有以下内容(感谢卢克):
All type-specific regexes posted so far are failing on the "type 0" Nil UUID, defined in 4.1.7 of the RFC as:
To modify Wolf's answer:
Or, to properly exclude a "type 0" without all zeros, we have the following (thanks to Luke):
如果您使用uuid包,您可以导入验证并将id传递给它
if you use the uuid package, you can import the validate and pass the id into it
如果有人使用 yup JavaScript 模式验证器库,则可以使用以下代码实现此功能。
If someone is using yup , JavaScript schema validator library, This functionality can be achieved with below code.
上述答案的稍微修改版本以更简洁的方式编写。这将验证带有连字符的任何 GUID(但是可以轻松修改以使连字符可选)。这也将支持大写和小写字符,这已成为惯例,无论规范如何:
这里的关键是下面的重复部分,
它简单地重复 4 个字符模式 3 次
A slightly modified version of the above answers written in a more concise way. This will validate any GUID with hyphens (however easily modified to make hyphens optional). This will also support upper and lower case characters which has become the convention regardless of the specification:
The key here is the repeating part below
Which simply repeats the 4 char patterns 3 times
目前,UUID 在 RFC4122 中指定。一个经常被忽视的边缘情况是 NIL UUID,此处指出。以下正则表达式考虑了这一点,并将返回 NIL UUID 的匹配项。请参阅下文了解仅接受非 NIL UUID 的 UUID。这两种解决方案均适用于版本 1 至 5(请参阅第三块的第一个字符)。
因此,要验证 UUID...
...确保您拥有规范格式的 UUID,该 UUID 为版本 1 至 5,并且是符合 RFC4122 的适当变体。
注意:大括号
{
和}
不是规范的。它们是某些系统和用途的产物。轻松修改上述正则表达式即可满足原问题的要求。
提示:正则表达式组/捕获
为了避免匹配 NIL UUID:
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...
...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:
如果您想检查或验证特定的 UUID 版本,这里是相应的正则表达式。
版本号是第三组的第一个字符:
[VERSION_NUMBER][0-9A-F]{3}
:UUID v1:
<前><代码>/^[0-9A-F]{8}-[0-9A-F]{4}-[1][0-9A-F]{3}-[89AB][0- 9A-F]{3}-[0-9A-F]{12}$/i
UUID v2:
<前><代码>/^[0-9A-F]{8}-[0-9A-F]{4}-[2][0-9A-F]{3}-[89AB][0- 9A-F]{3}-[0-9A-F]{12}$/i
UUID v3:
<前><代码>/^[0-9A-F]{8}-[0-9A-F]{4}-[3][0-9A-F]{3}-[89AB][0- 9A-F]{3}-[0-9A-F]{12}$/i
UUID v4:
<前><代码>/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0- 9A-F]{3}-[0-9A-F]{12}$/i
UUID v5:
<前><代码>/^[0-9A-F]{8}-[0-9A-F]{4}-[5][0-9A-F]{3}-[89AB][0- 9A-F]{3}-[0-9A-F]{12}$/i
If you want to check or validate a specific UUID version, here are the corresponding regexes.
The version number is the first character of the third group :
[VERSION_NUMBER][0-9A-F]{3}
:UUID v1 :
UUID v2 :
UUID v3 :
UUID v4 :
UUID v5 :
正则表达式来救援
或用括号
regex to the rescue
or with brackets