我的函数需要替换字符串中的标签,如果其中提取的数据有url。 例如:vb.net正则表达式 - 替换标签而不替换span标签
www.cnn.com
这工作正常,但是当我有这样一个字符串:
<a href=www.cnn.com><span style="color: rgb(255, 0, 0);">www.cnn.com</span></a>
我只得到:
www.cnn.com
<a href=www.cnn.com>www.cnn.com</a>
将被取代
当我真的想要sta y与:
<span style="color: rgb(255, 0, 0);">www.cnn.com</span>
我需要添加到它的代码工作?
这是我的函数:
Dim ret As String = text
'If it looks like a URL
Dim regURL As New Regex("(www|\.org\b|\.com\b|http)")
'Gets a Tags regex
Dim rxgATags = New Regex("<[^>]*>", RegexOptions.IgnoreCase)
'Gets all matches of <a></a> and adds them to a list
Dim matches As MatchCollection = Regex.Matches(ret, "<a\b[^>]*>(.*?)</a>")
'for each <a></a> in the text check it's content, if it looks like URL then delete the <a></a>
For Each m In matches
'tmpText holds the data extracted within the a tags. /visit at.../www.applyhere.com
Dim tmpText = rxgATags.Replace(m.ToString, "")
If regURL.IsMatch(tmpText) Then
ret = ret.Replace(m.ToString, tmpText)
End If
Next
Return ret
使用此“@”?a\b[^>] *>“'正则表达式。 – 2015-04-05 11:17:20