在我的项目中,我UUID.fromString()习惯将字符串转换为UUID,但是如果字符串不是UUIDtype,它将抛出exception,那么如何验证该字符串?
UUID.fromString()
UUID
exception
您应该使用正则表达式进行验证,例如:
/^[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}$/
用鸡蛋测试 01234567-9ABC-DEF0-1234-56789ABCDEF0
01234567-9ABC-DEF0-1234-56789ABCDEF0
或带括号
/^\{?[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}\}?$/