【问题标题】:Validate UUID without divider "-" [duplicate]验证不带分隔符“-”的 UUID [重复]
【发布时间】:2014-11-27 14:21:12
【问题描述】:

我获得格式为“005056963AB75FD48BDC59C100314C40”的 UUID,并希望对其进行验证。我试过这样的代码:

public boolean isUUID(String uuid){

    try {
        UUID.fromString(uuid);
    } catch (Exception e) {
        return false;
    }
    return true;

}

但这会告诉我“005056963AB75FD48BDC59C100314C40”不是有效的 ID。另一方面,http://guid.us/Test/GUID 网站告诉我它是,并给我一个添加了“-”的 UUID。 有没有一种优雅的方法可以在 java 中验证这个 UUID,还是我必须手动将“-”添加到正确的位置?

【问题讨论】:

    标签: java regex validation uuid


    【解决方案1】:

    尝试正则表达式

        uuid = uuid.replaceAll("(.{8})(.{4})(.{4})(.{4})(.+)", "$1-$2-$3-$4-$5");
        UUID.fromString(uuid);
    

    【讨论】:

      【解决方案2】:

      您可以使用此 Java 正则表达式代码将连字符放在 UUID 字符串中的正确位置:

      String s = "005056963AB75FD48BDC59C100314C40";
      s = s.replaceAll(
              "(?i)([a-f0-9]{8})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{12})", 
              "$1-$2-$3-$4-$5");
      
      System.out.printf("UUID: [%s]%n", s);
      //=> 00505696-3AB7-5FD4-8BDC-59C100314C40
      

      更新:非正则表达式解决方案(感谢@atamanroman)

      StringBuilder sbr = new StringBuilder(s);
      for(int i=8, j=0; i<=20; i+=4, j++)
          sbr.insert(i+j, '-');
      
      UUID uuid = UUID.fromString( sbr.toString() );
      

      【讨论】:

      • 我更喜欢StringBuilder#insert(pos, "-") 而不是那个疯狂的正则表达式。
      【解决方案3】:

      要在 UUID 字符串中添加破折号,我建议使用 StringBuilder#insert(int offset, chat c),正如 @atamanroman 所说。

      final String uuid = new StringBuilder("005056963AB75FD48BDC59C100314C40")
              .insert(20, '-')
              .insert(16, '-')
              .insert(12, '-')
              .insert(8, '-')
              .toString();
      
      System.out.println(uuid);
      

      【讨论】:

        【解决方案4】:

        这是一个验证不带连字符的 UUID 字符串的函数示例。它更快,因为它不使用正则表达式或缓冲。没有连字符的 UUID 只是 32 个十六进制字符的字符串。

        public class Example1 {
            /**
             * Validate UUID without hyphens.
             * 
             * @param uuid a uuid string
             * @return true if valid
             */
            public static boolean isValid(String uuid) {
        
                if (uuid == null || uuid.length() != 32) {
                    return false;
                }
        
                final char[] chars = uuid.toCharArray();
                for (int i = 0; i < chars.length; i++) {
                    final int c = chars[i];
                    if (!((c >= 0x30 && c <= 0x39) || (c >= 0x61 && c <= 0x66) || (c >= 0x41 && c <= 0x46))) {
                        // ASCII codes: 0-9, a-f, A-F
                        return false;
                    }
                }
                
                return true;
            }
        
            public static void main(String[] args) {
                String uuid = "005056963AB75FD48BDC59C100314C40";
                if(isValid(uuid)) {
                    // do something
                }
            }
        }
        

        您还可以使用库 uuid-creator 中的 UuidValidator.isValid()。它验证带有或不带有连字符的 UUID 字符串。这是另一个例子:

        import com.github.f4b6a3.uuid.util.UuidValidator;
        
        public class Example2 {
        
            public static void main(String[] args) {
                String uuid = "005056963AB75FD48BDC59C100314C40";
                if(UuidValidator.isValid(uuid)) {
                    // do something
                }
            }
        }
        

        但是如果你只是想把一个字符串转换成一个UUID,你可以使用UuidCreator.fromString()。此函数还验证输入。

        import java.util.UUID;
        import com.github.f4b6a3.uuid.util.UuidValidator;
        
        public class Example3 {
            public static void main(String[] args) {
                String uuidString = "005056963AB75FD48BDC59C100314C40";
                UUID uuid = UuidCreator.fromString(uuidString);
            }
        }
        

        项目页面:https://github.com/f4b6a3/uuid-creator

        【讨论】:

          猜你喜欢
          • 2013-01-13
          • 1970-01-01
          • 2015-11-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多