.net 提取并替换命名组regex

.net 提取并替换命名组regex,.net,regex,vb.net,.net,Regex,Vb.net,我能够提取html字符串中锚的href值。现在,我想要实现的是提取href值并用新GUID替换该值。我需要返回替换的html字符串和提取的href值列表及其对应的GUID 提前谢谢 我现有的代码如下: Dim sPattern As String = "<a[^>]*href\s*=\s*((\""(?<URL>[^\""]*)\"")|(\'(?<URL>[^\']*)\')|(?<URL>[^\s]* ))" Dim matches As M

我能够提取html字符串中锚的href值。现在,我想要实现的是提取href值并用新GUID替换该值。我需要返回替换的html字符串和提取的href值列表及其对应的GUID

提前谢谢

我现有的代码如下:

Dim sPattern As String = "<a[^>]*href\s*=\s*((\""(?<URL>[^\""]*)\"")|(\'(?<URL>[^\']*)\')|(?<URL>[^\s]* ))"

Dim matches As MatchCollection = Regex.Matches(html, sPattern, RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace)

If Not IsNothing(matches) AndAlso matches.Count > 0 Then
    Dim urls As List(Of String) = New List(Of String)

    For Each m As Match In matches
      urls.Add(m.Groups("URL").Value)
    Next
End If
Dim sPattern As String=“


您可以这样做:

Dim pattern As String = "<a[^>]*href\s*=\s*((\""(?<URL>[^\""]*)\"")|(\'(?<URL>[^\']*)\')|(?<URL>[^\s]* ))"
Dim urls As New Dictionary(Of Guid, String)
Dim evaluator As MatchEvaluator = Function(m)
    Dim g As Guid = Guid.NewGuid()
    Dim url = m.Groups("URL").Value
    urls.Add(g, url)
    Return m.Value.Replace(url, g.ToString())
End Function

Dim newHtml = Regex.Replace(html, pattern, evaluator)
URL
字典包含以下条目:

329eb2c4-ee51-49fa-a8cd-2de319c3dbad: http://www.google.com
77268e2d-87c4-443c-980c-9188e22f8496: http://www.yahoo.com
2941f77a-a143-4990-8ad7-3ef56972a8d4: http://www.apple.com

顺便说一下,请注意……像这样的工具更合适。

您可以这样做:

Dim pattern As String = "<a[^>]*href\s*=\s*((\""(?<URL>[^\""]*)\"")|(\'(?<URL>[^\']*)\')|(?<URL>[^\s]* ))"
Dim urls As New Dictionary(Of Guid, String)
Dim evaluator As MatchEvaluator = Function(m)
    Dim g As Guid = Guid.NewGuid()
    Dim url = m.Groups("URL").Value
    urls.Add(g, url)
    Return m.Value.Replace(url, g.ToString())
End Function

Dim newHtml = Regex.Replace(html, pattern, evaluator)
URL
字典包含以下条目:

329eb2c4-ee51-49fa-a8cd-2de319c3dbad: http://www.google.com
77268e2d-87c4-443c-980c-9188e22f8496: http://www.yahoo.com
2941f77a-a143-4990-8ad7-3ef56972a8d4: http://www.apple.com

顺便说一句,请注意……像这样的工具更合适。

废话。只注意到我在事后复制了你的答案。+1:)废话。只注意到我在事后复制了你的答案。+1:)