【问题标题】:How can I find all the Guids in some text?如何在某些文本中找到所有指南?
【发布时间】:2009-04-04 06:02:42
【问题描述】:

我的数据库中有一堆网页内容,链接如下:

<a href="/11ecfdc5-d28d-4121-b1c9-1f898ac0b72e">Link</a>

该 Guid 唯一标识符是同一数据库中另一个页面的 ID。

我想抓取这些页面并检查是否有损坏的链接。

为此,我需要一个可以返回页面上所有 Guid 列表的函数:

函数 FindGuids(ByVal Text As String) As Collections.Generic.List(Of Guid) ... 结束功能

我认为这是正则表达式的工作。但是,我不知道语法。

【问题讨论】:

    标签: .net vb.net regex guid


    【解决方案1】:
    函数 FindGuids(ByVal Text As String) As List(Of Guid) Dim Guids 作为新列表(Guid) 暗模式作为字符串 = "[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}" 对于每个 m 作为 Regex.Matches(Text, Pattern) 中的匹配项 Guids.Add(新的 Guid(m.Value)) 下一个 返回指南 结束功能

    【讨论】:

      【解决方案2】:

      [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0- 9a-f]{12}

      【讨论】:

        【解决方案3】:

        建议您获取expresso 的免费副本并学习构建它们!

        这是一个没有优化的 10 秒尝试,检查大小写并创建一个编号的捕获组:

        ([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})
        

        然后你只需要遍历匹配的组...

        【讨论】:

          【解决方案4】:

          有更简单的方法来检查损坏的链接....例如,我认为http://www.totalvalidator.com/ 会这样做:D

          这也有帮助

          static Regex isGuid = 
              new Regex(@"^(\{){0,1}[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}(\}){0,1}$", RegexOptions.Compiled);
          

          然后

          static bool IsGuid(string candidate, out Guid output)
          {
          bool isValid = false;
          output=Guid.Empty;
          if(candidate!=null)
          {
          
           if (isGuid.IsMatch(candidate))
           {
            output=new Guid(candidate);
            isValid = true;
           }
          }
          return isValid;
          

          }

          【讨论】:

          • 看起来很方便。但是这个网站的很多页面都需要登录,并且还有其他我必须处理的业务规则。
          • 总验证器(高级)也将进行身份验证!
          • 我认为它实际上是Pro(非高级)版本
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-17
          • 2023-04-07
          • 2019-10-18
          • 1970-01-01
          • 1970-01-01
          • 2019-08-05
          相关资源
          最近更新 更多