如何检查字符串是否是vbscript中的有效GUID?有没有人写过IsGuid方法?

有帮助吗?

解决方案

这类似于同样的问题在c#中。这是你需要的正则表达式......

^ [A-发f0-9] {32} $ | ^({|?()[A-发f0-9] {8} - ([A-发f0-9] {4 } - ){3} [A-Fa-f0-9] {12}(} |))?$ | ^({)?[0xA-Fa-f0-9] {3,10}(,{0, 1} [0xA-Fa-f0-9] {3,6}){2},{0,1}({)([0xA-Fa-f0-9] {3,4},{0,1} ){7} [是0xA发-f0-9] {3,4}(}})$

但这仅适用于初学者。您还必须验证日期/时间等各个部分是否在可接受的范围内。要了解测试有效GUID的复杂程度,请查看其中一个Guid构造函数的源代码。

其他提示

此功能适用于经典ASP:

Function isGUID(byval strGUID)
      if isnull(strGUID) then
        isGUID = false
        exit function
      end if
      dim regEx
      set regEx = New RegExp
      regEx.Pattern = "^({|\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\))?<*>quot;
      isGUID = regEx.Test(strGUID)
      set RegEx = nothing
End Function

请参阅检查GUID

在VBScript中,您可以使用RegExp对象使用正则表达式匹配字符串。

Techek的功能在经典ASP(vbScript)中对我不起作用。由于一些奇怪的原因,它总是返回True。通过一些小的改动它确实有效。见下文

Function isGUID(byval strGUID)
  if isnull(strGUID) then
    isGUID = false
    exit function
  end if
  dim regEx
  set regEx = New RegExp
  regEx.Pattern = "{[0-9A-Fa-f-]+}"
  isGUID = regEx.Test(strGUID)
  set RegEx = nothing
End Function

还有另一种解决方案:

try
{
  Guid g = new Guid(stringGuid);
  safeUseGuid(stringGuid); //this statement will execute only if guid is correct
}catch(Exception){}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top