如果我有一个字符串"key:<String || UUID>"
,有没有一种方法可以提取字符串并区分:
字符串或 UUID 之后的部分?
示例:在此key:863864947148451183L
和key:1234
可能是通过使用 UUID 的大小或字节数?
唯一万无一失的方法是使用
UUID.fromString(yourString);
它会返回一个
java.lang.IllegalArgumentException: Invalid UUID string: "someString"
处理异常。
javadoc解释了UUID 格式。
使用异常来替换逻辑从来都不是一个好的选择。在这种情况下,从逻辑上讲,使用异常也是错误的。
首先必须考虑为什么抛出异常,以及抛出异常的行为是否依赖于 JVM。
例如,我的 JDK 在评估是否引发异常时指向以下逻辑
String[] var1 = var0.split("-");
if (var1.length != 5) {
throw new IllegalArgumentException("Invalid UUID string: " + var0);
}
可以看出,这是一种简单而快速的检查。
Long.decode
,如前所述,还会引发一个异常 NumberFormatException,这可能是一个很大的延伸,以将其理解为一种验证机制。
即使那些开始变得丑陋,如果其中任何一个无效,人们仍然可能使用导致 UUID 无效的理性。
但事实是,如果一个人选择使用这个异常来构建逻辑,那么可以从这个异常中得到真正的测试,如果它可以转换为 5 个分隔的数字,那么说 String 是一个 UUID 就足够了经过 ”-”。(!)
当然,RFC 4122 不止于此。
但让我们关注 Java 和 UUID 类。 https://docs.oracle.com/javase/8/docs/api/java/util/UUID.html
可以简单地使用文档格式来创建验证器
UUID = <time_low> "-" <time_mid> "-" <time_high_and_version> "-" <variant_and_sequence> "-" <node> time_low = 4*<hexOctet> time_mid = 2*<hexOctet> time_high_and_version = 2*<hexOctet> variant_and_sequence = 2*<hexOctet> node = 6*<hexOctet> hexOctet = <hexDigit><hexDigit> hexDigit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "a" | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F"
这可以用java翻译成
private static final String TIME_LOW = "[0-9a-fA-F]{8}";
private static final String TIME_MID = "[0-9a-fA-F]{4}";
private static final String TIME_HIGH_AND_VERSION = "[0-9a-fA-F]{4}";
private static final String VARIANT_AND_SEQUENCE = "[0-9a-fA-F]{4}";
private static final String NODE = "[0-9a-fA-F]{12}";
private static final Pattern UUID_PATTERN = Pattern.compile( "^" + TIME_LOW + "-" + TIME_MID + "-" + TIME_HIGH_AND_VERSION + "-" + VARIANT_AND_SEQUENCE + "-" + NODE + "$" );
当然,这只是外部描述。
这是第一次尝试,与文档一致。但是根据 RFC 和其他文档,由于 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)^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-5][0-9a-f]{3}-?[089ab][0-9a-f]{3}-?[0-9a-f]{12}$"
和其他版本特定的正则表达式......
但是这是不考虑Java还提供以下方法
UUID uuid = new UUID(long mostSigBits, long leastSigBits);
这会导致任意类型,并且可以用来生成字符串;这些正则表达式会导致 UUID 出现如下问题:
@Override
public String generate() {
UUID uuid = new UUID(
SecureRandomUtil.nextLong(), SecureRandomUtil.nextLong());
return uuid.toString();
}
如果值得继续验证版本和变体,这取决于您的需要。除非您确定 UUID 在版本 3 或 4 中(这是文档中明确提到的版本),或者您知道版本范围...
更多代码......正如预期的那样,只有一个有效
public class UuidValidatorTest {
@Test
public void isUuid( ) {
assertEquals( UuidValidator.isUuid( "000-000-000-0-0" ), false );
assertEquals( UuidValidator.isUuid( "1-1-1-1-1" ), false );
assertEquals( UuidValidator.isUuid( "1-1-1-1-1-1" ), false );
assertEquals( UuidValidator.isUuid( "0-0-0-0" ), false );
assertEquals( UuidValidator.isUuid( "a-a-a-a-a" ), false );
assertEquals( UuidValidator.isUuid( "#-b-c-4-3" ), false );
assertEquals( UuidValidator.isUuid( "#-b-c-43" ), false );
assertEquals( UuidValidator.isUuid( "#-b-c-4-3" ), false );
for ( int i = 0; i < 100; i++ ) {
assertEquals( UuidValidator.isUuid( UUID.randomUUID( )
.toString( ) ), true );
}
assertEquals( UuidValidator.isUuid( "0297bba3-910e-6672-89e8-c3eacecfe1fa" ), true );
assertEquals( UuidValidator.isUuid( "02a67f91-c4ca-e886-309a-35cf7079babd" ), true );
assertEquals( UuidValidator.isUuid( "02f07ba0-72a5-cdcd-d41e-e3b4859623b5" ), true );
assertEquals( UuidValidator.isUuid( "0457a05a-4104-a4e9-ae09-a32491d42c5a" ), true );
assertEquals( UuidValidator.isUuid( "07ee5f9a-aa75-ec6f-f73e-7e99d9a40354" ), true );
assertEquals( UuidValidator.isUuid( "08623b7a-75a3-bd91-8d95-eb46eabfd53d" ), true );
assertEquals( UuidValidator.isUuid( "089db790-954c-8db6-f5f6-bbb15e6ae860" ), true );
assertEquals( UuidValidator.isUuid( "08e4be51-4799-2ef9-d2ee-1981fb00a41e" ), true );
assertEquals( UuidValidator.isUuid( "0acf3eed-3455-e9b2-ee8e-5266e6a4d659" ), true );
assertEquals( UuidValidator.isUuid( "0ff8fe99-ce2d-a492-78c6-4a5895f6ad10" ), true );
assertEquals( UuidValidator.isUuid( "15d95f4b-958b-501f-5f73-997070c06a1a" ), true );
assertEquals( UuidValidator.isUuid( "169269e9-044c-63d4-99b4-3661261b3be9" ), true );
assertEquals( UuidValidator.isUuid( "1771bfb3-2c98-b0e9-271f-f59a9a314074" ), true );
assertEquals( UuidValidator.isUuid( "1992d0b4-2c46-6912-f1ec-56f03e2b0ea7" ), true );
assertEquals( UuidValidator.isUuid( "19fc7632-ac67-8cda-7d04-a549cee0585d" ), true );
assertEquals( UuidValidator.isUuid( "1a4ae1c9-b225-b8b2-8dbf-6ba3f497c939" ), true );
assertEquals( UuidValidator.isUuid( "1d71875f-7da2-52c8-ca28-6bd57ac6fb34" ), true );
assertEquals( UuidValidator.isUuid( "1e2dafca-7dd8-ffca-9bf8-a4e6132b0437" ), true );
assertEquals( UuidValidator.isUuid( "1e494bfe-cd24-c2dc-4ac8-0192a1766758" ), true );
assertEquals( UuidValidator.isUuid( "1e50da64-ee2a-769d-187b-cafb04befece" ), true );
assertEquals( UuidValidator.isUuid( "1f856c29-b2f7-d07d-7c2a-611e44628810" ), true );
assertEquals( UuidValidator.isUuid( "2311842f-c032-0cec-3967-4f33b45cc24c" ), true );
assertEquals( UuidValidator.isUuid( "2575d3db-3c8c-21ab-a458-dbc7a5655418" ), true );
assertEquals( UuidValidator.isUuid( "25dc750d-afe7-3394-b199-760486888e74" ), true );
assertEquals( UuidValidator.isUuid( "26293add-e373-90c9-4503-917c448bdcbd" ), true );
assertEquals( UuidValidator.isUuid( "2dc8c6df-ab98-3546-935f-4ba1c3d6fe74" ), true );
assertEquals( UuidValidator.isUuid( "30334014-fac7-1411-113d-1367f1eb6a1b" ), true );
assertEquals( UuidValidator.isUuid( "3109e737-a1a2-cc87-c1dd-111329b3b1a4" ), true );
assertEquals( UuidValidator.isUuid( "32e98007-0a3e-5242-8d0e-f2ae48bf0864" ), true );
assertEquals( UuidValidator.isUuid( "362a1961-3da4-28c4-4067-0be4ce31c159" ), true );
assertEquals( UuidValidator.isUuid( "3b95871a-4dbf-395f-5c9f-e5f323bdfd13" ), true );
assertEquals( UuidValidator.isUuid( "3ef8cc6c-1296-d6d3-903f-d92240885cc9" ), true );
assertEquals( UuidValidator.isUuid( "3fa4ef78-33f3-3e67-9eae-6a67c5716fa9" ), true );
assertEquals( UuidValidator.isUuid( "4046d469-f1b4-ebfc-7264-500d3b4da506" ), true );
assertEquals( UuidValidator.isUuid( "42b4f4c0-6b20-932f-f7c7-c2a2f62208b6" ), true );
assertEquals( UuidValidator.isUuid( "42fb4ec7-a623-665d-73ac-14b4559ce1f0" ), true );
assertEquals( UuidValidator.isUuid( "443e8753-01c4-7901-e839-3ac87573e2b5" ), true );
assertEquals( UuidValidator.isUuid( "446f9fdb-0676-680a-ee49-f252ed4ec538" ), true );
assertEquals( UuidValidator.isUuid( "464eb3e0-dff8-b68a-24a5-70a1753594fc" ), true );
assertEquals( UuidValidator.isUuid( "46ba5592-d353-37a3-842d-1bcc04d6deb8" ), true );
assertEquals( UuidValidator.isUuid( "48699a39-d976-6ef4-0ae0-241c931637d9" ), true );
assertEquals( UuidValidator.isUuid( "4a5dbe49-7214-0d5d-a7b1-a5619b60f400" ), true );
assertEquals( UuidValidator.isUuid( "4a8114ab-173a-d83b-b696-f7db487d2708" ), true );
assertEquals( UuidValidator.isUuid( "4f0d7370-763b-7ea4-452b-39269e788a8c" ), true );
assertEquals( UuidValidator.isUuid( "5086a7dc-c810-6ac4-6951-4212dde66537" ), true );
assertEquals( UuidValidator.isUuid( "5237e6b4-a5d8-4eab-1acf-fff899453d34" ), true );
assertEquals( UuidValidator.isUuid( "52f9a633-8ba1-1001-7d27-0c38a2e28232" ), true );
assertEquals( UuidValidator.isUuid( "53dc03f1-a77e-2f43-d46c-6f166b14f540" ), true );
assertEquals( UuidValidator.isUuid( "56634e53-730f-7a7e-4476-21b06fab20d9" ), true );
assertEquals( UuidValidator.isUuid( "575215c7-69f6-7211-ddb5-a87dd5f755e5" ), true );
assertEquals( UuidValidator.isUuid( "57dfaa94-150e-63e4-4172-bf042f27facf" ), true );
assertEquals( UuidValidator.isUuid( "5802ab41-2543-ecd6-8571-46e6ce389b06" ), true );
assertEquals( UuidValidator.isUuid( "5a7ed59e-8f1b-acde-6edb-3d116f3c99ed" ), true );
assertEquals( UuidValidator.isUuid( "5a8feb9f-09ba-c1df-9407-a28aa743e797" ), true );
assertEquals( UuidValidator.isUuid( "5d98e524-02f9-7dec-0838-feec68e0b95d" ), true );
assertEquals( UuidValidator.isUuid( "5e92060a-a504-afce-3c27-7e1269f90999" ), true );
assertEquals( UuidValidator.isUuid( "5edab579-10b4-7a77-b7cc-7a9688d5e63a" ), true );
assertEquals( UuidValidator.isUuid( "66716e2e-dd18-8782-e33e-4fad7ec299ea" ), true );
assertEquals( UuidValidator.isUuid( "680065c7-4897-46be-75bf-9c74167be8f1" ), true );
assertEquals( UuidValidator.isUuid( "68e41661-9a24-89a1-489e-93ad934b0846" ), true );
assertEquals( UuidValidator.isUuid( "6ac02e05-8bc7-f402-9401-4d54a61c44f6" ), true );
assertEquals( UuidValidator.isUuid( "6b8e08c5-3ee6-b616-2646-38c7d4faa04c" ), true );
assertEquals( UuidValidator.isUuid( "70f13ff1-e205-98f6-a1ff-ccb4af8255d4" ), true );
assertEquals( UuidValidator.isUuid( "724c5782-8c40-8b80-0012-e1d832b9e066" ), true );
assertEquals( UuidValidator.isUuid( "765519f4-51e9-0fd9-2f21-ce0b874b5482" ), true );
assertEquals( UuidValidator.isUuid( "77b3acfd-b7eb-083e-c51c-7d5b30bad312" ), true );
assertEquals( UuidValidator.isUuid( "783d2bc2-e745-d1ae-4cf6-c669e538cf21" ), true );
assertEquals( UuidValidator.isUuid( "7c1d5469-df85-d157-deac-15e86e721f06" ), true );
assertEquals( UuidValidator.isUuid( "7d63c847-037f-7a06-e7ac-981338ca4935" ), true );
assertEquals( UuidValidator.isUuid( "7e60b2d4-94e3-e735-c8a4-16032886b94c" ), true );
assertEquals( UuidValidator.isUuid( "7fed1a4e-59e0-9809-f857-363bbc526604" ), true );
assertEquals( UuidValidator.isUuid( "8228784b-3090-e4a5-185d-97278db07ded" ), true );
assertEquals( UuidValidator.isUuid( "88aedd2a-9785-5925-7105-455df71283c2" ), true );
assertEquals( UuidValidator.isUuid( "89af347d-f602-d4a1-0b1b-545b142d24fd" ), true );
assertEquals( UuidValidator.isUuid( "8a43653f-9268-35c4-2cf6-ebc682d0b68d" ), true );
assertEquals( UuidValidator.isUuid( "8ba6284f-3829-7017-a63e-5b3d54002ac2" ), true );
assertEquals( UuidValidator.isUuid( "8cb60b26-ddfa-ba7a-fb96-e088a94d78de" ), true );
assertEquals( UuidValidator.isUuid( "8de06bac-305e-e1e6-809b-5c7e00c6e662" ), true );
assertEquals( UuidValidator.isUuid( "8f3497bc-037e-d365-f3f2-1adb1c2abc45" ), true );
assertEquals( UuidValidator.isUuid( "92d4d7d9-2c8c-9703-ba4f-3e69fd420a99" ), true );
assertEquals( UuidValidator.isUuid( "92fff203-a3dd-6aa1-4deb-60e1ed929b0c" ), true );
assertEquals( UuidValidator.isUuid( "99d7a461-b9a4-1ba3-0380-638148f097aa" ), true );
assertEquals( UuidValidator.isUuid( "99e2be43-3064-5a6e-9d7f-0aab0ef62733" ), true );
assertEquals( UuidValidator.isUuid( "9e7b55fb-7f9f-6aeb-90b3-4bc331cfae66" ), true );
assertEquals( UuidValidator.isUuid( "a04065b8-c223-77a6-85c5-588ac84f8acf" ), true );
assertEquals( UuidValidator.isUuid( "a1299e22-f2ba-437c-9d3a-cfcee841c76a" ), true );
assertEquals( UuidValidator.isUuid( "a1d5a723-8a39-19ac-339e-78ac35ce96f4" ), true );
assertEquals( UuidValidator.isUuid( "a394cfcd-867b-3ef2-2d0b-3b96ba2e40a8" ), true );
assertEquals( UuidValidator.isUuid( "a3cce812-b718-2ddf-b7de-acd011e8f24a" ), true );
assertEquals( UuidValidator.isUuid( "a4d4a700-7e9f-f4f7-9be3-4ed87b4ba157" ), true );
assertEquals( UuidValidator.isUuid( "a88cb5ec-bc35-4b60-0840-4d541caf6258" ), true );
assertEquals( UuidValidator.isUuid( "a952b4c1-7377-cce5-0b81-c770cd0c884a" ), true );
assertEquals( UuidValidator.isUuid( "abf7d3d0-ae8c-1495-6a44-27cd098f0b2f" ), true );
assertEquals( UuidValidator.isUuid( "ace167ab-2c57-d4a5-0e2a-d486445870cb" ), true );
assertEquals( UuidValidator.isUuid( "ad6f7597-0aef-9ef0-e293-a6ce1583eb7e" ), true );
assertEquals( UuidValidator.isUuid( "b0685971-fee9-38a2-ce30-17b4bd51c8d1" ), true );
}
@Test
public void isUuidWithException( ) {
assertEquals( UuidValidator.isUuidWithException( "000-000-000-0-0" ), false );
assertEquals( UuidValidator.isUuidWithException( "1-1-1-1-1" ), false );
assertEquals( UuidValidator.isUuidWithException( "1-1-1-1-1-1" ), false );
assertEquals( UuidValidator.isUuidWithException( "0-0-0-0" ), false );
assertEquals( UuidValidator.isUuidWithException( "a-a-a-a-a" ), false );
assertEquals( UuidValidator.isUuidWithException( "#-b-c-4-3" ), false );
assertEquals( UuidValidator.isUuidWithException( "#-b-c-43" ), false );
assertEquals( UuidValidator.isUuidWithException( "#-b-c-4-3" ), false );
for ( int i = 0; i < 100; i++ ) {
assertEquals( UuidValidator.isUuidWithException( UUID.randomUUID( )
.toString( ) ), true );
}
assertEquals( UuidValidator.isUuidWithException( "0297bba3-910e-6672-89e8-c3eacecfe1fa" ), true );
assertEquals( UuidValidator.isUuidWithException( "02a67f91-c4ca-e886-309a-35cf7079babd" ), true );
assertEquals( UuidValidator.isUuidWithException( "02f07ba0-72a5-cdcd-d41e-e3b4859623b5" ), true );
assertEquals( UuidValidator.isUuidWithException( "0457a05a-4104-a4e9-ae09-a32491d42c5a" ), true );
assertEquals( UuidValidator.isUuidWithException( "07ee5f9a-aa75-ec6f-f73e-7e99d9a40354" ), true );
assertEquals( UuidValidator.isUuidWithException( "08623b7a-75a3-bd91-8d95-eb46eabfd53d" ), true );
assertEquals( UuidValidator.isUuidWithException( "089db790-954c-8db6-f5f6-bbb15e6ae860" ), true );
assertEquals( UuidValidator.isUuidWithException( "08e4be51-4799-2ef9-d2ee-1981fb00a41e" ), true );
assertEquals( UuidValidator.isUuidWithException( "0acf3eed-3455-e9b2-ee8e-5266e6a4d659" ), true );
assertEquals( UuidValidator.isUuidWithException( "0ff8fe99-ce2d-a492-78c6-4a5895f6ad10" ), true );
assertEquals( UuidValidator.isUuidWithException( "15d95f4b-958b-501f-5f73-997070c06a1a" ), true );
assertEquals( UuidValidator.isUuidWithException( "169269e9-044c-63d4-99b4-3661261b3be9" ), true );
assertEquals( UuidValidator.isUuidWithException( "1771bfb3-2c98-b0e9-271f-f59a9a314074" ), true );
assertEquals( UuidValidator.isUuidWithException( "1992d0b4-2c46-6912-f1ec-56f03e2b0ea7" ), true );
assertEquals( UuidValidator.isUuidWithException( "19fc7632-ac67-8cda-7d04-a549cee0585d" ), true );
assertEquals( UuidValidator.isUuidWithException( "1a4ae1c9-b225-b8b2-8dbf-6ba3f497c939" ), true );
assertEquals( UuidValidator.isUuidWithException( "1d71875f-7da2-52c8-ca28-6bd57ac6fb34" ), true );
assertEquals( UuidValidator.isUuidWithException( "1e2dafca-7dd8-ffca-9bf8-a4e6132b0437" ), true );
assertEquals( UuidValidator.isUuidWithException( "1e494bfe-cd24-c2dc-4ac8-0192a1766758" ), true );
assertEquals( UuidValidator.isUuidWithException( "1e50da64-ee2a-769d-187b-cafb04befece" ), true );
assertEquals( UuidValidator.isUuidWithException( "1f856c29-b2f7-d07d-7c2a-611e44628810" ), true );
assertEquals( UuidValidator.isUuidWithException( "2311842f-c032-0cec-3967-4f33b45cc24c" ), true );
assertEquals( UuidValidator.isUuidWithException( "2575d3db-3c8c-21ab-a458-dbc7a5655418" ), true );
assertEquals( UuidValidator.isUuidWithException( "25dc750d-afe7-3394-b199-760486888e74" ), true );
assertEquals( UuidValidator.isUuidWithException( "26293add-e373-90c9-4503-917c448bdcbd" ), true );
assertEquals( UuidValidator.isUuidWithException( "2dc8c6df-ab98-3546-935f-4ba1c3d6fe74" ), true );
assertEquals( UuidValidator.isUuidWithException( "30334014-fac7-1411-113d-1367f1eb6a1b" ), true );
assertEquals( UuidValidator.isUuidWithException( "3109e737-a1a2-cc87-c1dd-111329b3b1a4" ), true );
assertEquals( UuidValidator.isUuidWithException( "32e98007-0a3e-5242-8d0e-f2ae48bf0864" ), true );
assertEquals( UuidValidator.isUuidWithException( "362a1961-3da4-28c4-4067-0be4ce31c159" ), true );
assertEquals( UuidValidator.isUuidWithException( "3b95871a-4dbf-395f-5c9f-e5f323bdfd13" ), true );
assertEquals( UuidValidator.isUuidWithException( "3ef8cc6c-1296-d6d3-903f-d92240885cc9" ), true );
assertEquals( UuidValidator.isUuidWithException( "3fa4ef78-33f3-3e67-9eae-6a67c5716fa9" ), true );
assertEquals( UuidValidator.isUuidWithException( "4046d469-f1b4-ebfc-7264-500d3b4da506" ), true );
assertEquals( UuidValidator.isUuidWithException( "42b4f4c0-6b20-932f-f7c7-c2a2f62208b6" ), true );
assertEquals( UuidValidator.isUuidWithException( "42fb4ec7-a623-665d-73ac-14b4559ce1f0" ), true );
assertEquals( UuidValidator.isUuidWithException( "443e8753-01c4-7901-e839-3ac87573e2b5" ), true );
assertEquals( UuidValidator.isUuidWithException( "446f9fdb-0676-680a-ee49-f252ed4ec538" ), true );
assertEquals( UuidValidator.isUuidWithException( "464eb3e0-dff8-b68a-24a5-70a1753594fc" ), true );
assertEquals( UuidValidator.isUuidWithException( "46ba5592-d353-37a3-842d-1bcc04d6deb8" ), true );
assertEquals( UuidValidator.isUuidWithException( "48699a39-d976-6ef4-0ae0-241c931637d9" ), true );
assertEquals( UuidValidator.isUuidWithException( "4a5dbe49-7214-0d5d-a7b1-a5619b60f400" ), true );
assertEquals( UuidValidator.isUuidWithException( "4a8114ab-173a-d83b-b696-f7db487d2708" ), true );
assertEquals( UuidValidator.isUuidWithException( "4f0d7370-763b-7ea4-452b-39269e788a8c" ), true );
assertEquals( UuidValidator.isUuidWithException( "5086a7dc-c810-6ac4-6951-4212dde66537" ), true );
assertEquals( UuidValidator.isUuidWithException( "5237e6b4-a5d8-4eab-1acf-fff899453d34" ), true );
assertEquals( UuidValidator.isUuidWithException( "52f9a633-8ba1-1001-7d27-0c38a2e28232" ), true );
assertEquals( UuidValidator.isUuidWithException( "53dc03f1-a77e-2f43-d46c-6f166b14f540" ), true );
assertEquals( UuidValidator.isUuidWithException( "56634e53-730f-7a7e-4476-21b06fab20d9" ), true );
assertEquals( UuidValidator.isUuidWithException( "575215c7-69f6-7211-ddb5-a87dd5f755e5" ), true );
assertEquals( UuidValidator.isUuidWithException( "57dfaa94-150e-63e4-4172-bf042f27facf" ), true );
assertEquals( UuidValidator.isUuidWithException( "5802ab41-2543-ecd6-8571-46e6ce389b06" ), true );
assertEquals( UuidValidator.isUuidWithException( "5a7ed59e-8f1b-acde-6edb-3d116f3c99ed" ), true );
assertEquals( UuidValidator.isUuidWithException( "5a8feb9f-09ba-c1df-9407-a28aa743e797" ), true );
assertEquals( UuidValidator.isUuidWithException( "5d98e524-02f9-7dec-0838-feec68e0b95d" ), true );
assertEquals( UuidValidator.isUuidWithException( "5e92060a-a504-afce-3c27-7e1269f90999" ), true );
assertEquals( UuidValidator.isUuidWithException( "5edab579-10b4-7a77-b7cc-7a9688d5e63a" ), true );
assertEquals( UuidValidator.isUuidWithException( "66716e2e-dd18-8782-e33e-4fad7ec299ea" ), true );
assertEquals( UuidValidator.isUuidWithException( "680065c7-4897-46be-75bf-9c74167be8f1" ), true );
assertEquals( UuidValidator.isUuidWithException( "68e41661-9a24-89a1-489e-93ad934b0846" ), true );
assertEquals( UuidValidator.isUuidWithException( "6ac02e05-8bc7-f402-9401-4d54a61c44f6" ), true );
assertEquals( UuidValidator.isUuidWithException( "6b8e08c5-3ee6-b616-2646-38c7d4faa04c" ), true );
assertEquals( UuidValidator.isUuidWithException( "70f13ff1-e205-98f6-a1ff-ccb4af8255d4" ), true );
assertEquals( UuidValidator.isUuidWithException( "724c5782-8c40-8b80-0012-e1d832b9e066" ), true );
assertEquals( UuidValidator.isUuidWithException( "765519f4-51e9-0fd9-2f21-ce0b874b5482" ), true );
assertEquals( UuidValidator.isUuidWithException( "77b3acfd-b7eb-083e-c51c-7d5b30bad312" ), true );
assertEquals( UuidValidator.isUuidWithException( "783d2bc2-e745-d1ae-4cf6-c669e538cf21" ), true );
assertEquals( UuidValidator.isUuidWithException( "7c1d5469-df85-d157-deac-15e86e721f06" ), true );
assertEquals( UuidValidator.isUuidWithException( "7d63c847-037f-7a06-e7ac-981338ca4935" ), true );
assertEquals( UuidValidator.isUuidWithException( "7e60b2d4-94e3-e735-c8a4-16032886b94c" ), true );
assertEquals( UuidValidator.isUuidWithException( "7fed1a4e-59e0-9809-f857-363bbc526604" ), true );
assertEquals( UuidValidator.isUuidWithException( "8228784b-3090-e4a5-185d-97278db07ded" ), true );
assertEquals( UuidValidator.isUuidWithException( "88aedd2a-9785-5925-7105-455df71283c2" ), true );
assertEquals( UuidValidator.isUuidWithException( "89af347d-f602-d4a1-0b1b-545b142d24fd" ), true );
assertEquals( UuidValidator.isUuidWithException( "8a43653f-9268-35c4-2cf6-ebc682d0b68d" ), true );
assertEquals( UuidValidator.isUuidWithException( "8ba6284f-3829-7017-a63e-5b3d54002ac2" ), true );
assertEquals( UuidValidator.isUuidWithException( "8cb60b26-ddfa-ba7a-fb96-e088a94d78de" ), true );
assertEquals( UuidValidator.isUuidWithException( "8de06bac-305e-e1e6-809b-5c7e00c6e662" ), true );
assertEquals( UuidValidator.isUuidWithException( "8f3497bc-037e-d365-f3f2-1adb1c2abc45" ), true );
assertEquals( UuidValidator.isUuidWithException( "92d4d7d9-2c8c-9703-ba4f-3e69fd420a99" ), true );
assertEquals( UuidValidator.isUuidWithException( "92fff203-a3dd-6aa1-4deb-60e1ed929b0c" ), true );
assertEquals( UuidValidator.isUuidWithException( "99d7a461-b9a4-1ba3-0380-638148f097aa" ), true );
assertEquals( UuidValidator.isUuidWithException( "99e2be43-3064-5a6e-9d7f-0aab0ef62733" ), true );
assertEquals( UuidValidator.isUuidWithException( "9e7b55fb-7f9f-6aeb-90b3-4bc331cfae66" ), true );
assertEquals( UuidValidator.isUuidWithException( "a04065b8-c223-77a6-85c5-588ac84f8acf" ), true );
assertEquals( UuidValidator.isUuidWithException( "a1299e22-f2ba-437c-9d3a-cfcee841c76a" ), true );
assertEquals( UuidValidator.isUuidWithException( "a1d5a723-8a39-19ac-339e-78ac35ce96f4" ), true );
assertEquals( UuidValidator.isUuidWithException( "a394cfcd-867b-3ef2-2d0b-3b96ba2e40a8" ), true );
assertEquals( UuidValidator.isUuidWithException( "a3cce812-b718-2ddf-b7de-acd011e8f24a" ), true );
assertEquals( UuidValidator.isUuidWithException( "a4d4a700-7e9f-f4f7-9be3-4ed87b4ba157" ), true );
assertEquals( UuidValidator.isUuidWithException( "a88cb5ec-bc35-4b60-0840-4d541caf6258" ), true );
assertEquals( UuidValidator.isUuidWithException( "a952b4c1-7377-cce5-0b81-c770cd0c884a" ), true );
assertEquals( UuidValidator.isUuidWithException( "abf7d3d0-ae8c-1495-6a44-27cd098f0b2f" ), true );
assertEquals( UuidValidator.isUuidWithException( "ace167ab-2c57-d4a5-0e2a-d486445870cb" ), true );
assertEquals( UuidValidator.isUuidWithException( "ad6f7597-0aef-9ef0-e293-a6ce1583eb7e" ), true );
assertEquals( UuidValidator.isUuidWithException( "b0685971-fee9-38a2-ce30-17b4bd51c8d1" ), true );
}
}
使用 Scala,您可以使用这两个班轮:
val asTry = Try(UUID.fromString(str))
asTry.isSuccess && asTry.get.toString == str
比那个正则表达式干净得多:P
通过测试:
test("testIsValidUUID") {
isValidUUID("fooo") should be(false)
isValidUUID("1-1-1-1-1") should be(false)
isValidUUID(UUID.randomUUID().toString) should be(true)
}
在 Scala 中,我更喜欢这种方法:
Try(UUID.fromString(candidate))
.filter(_.toString == candidate)
.isSuccess
这样你就可以避免使用.get
您可以使用类验证器。
import { isUUID } from 'class-validator';
isUUID(user.id);