vb.net Regex - 替换标签而不替换跨度标签 [英] vb.net Regex - Replace a tags without replacing span tags
本文介绍了vb.net Regex - 替换标签而不替换跨度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果从字符串中提取的数据具有 url,我的函数需要替换字符串中的标签.例如:
My function needs to replace a tags from a string if the data extracted in it has a url. for example:
<a href=www.cnn.com>www.cnn.com</a>
将替换为:
www.cnn.com
这很好用,但是当我有一个像这样的字符串时:
That works fine but when i have a string like:
<a href=www.cnn.com><span style="color: rgb(255, 0, 0);">www.cnn.com</span></a>
我只得到:
www.cnn.com
当我真的想留下来时:
<span style="color: rgb(255, 0, 0);">www.cnn.com</span>
我需要在代码中添加什么才能使其工作?
What do i need to add to the code for it to work?
这是我的功能:
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
推荐答案
我将这个添加到我的代码中:
I add this to my code:
'Selects only the A tags without the data extracted between them
Dim rxgATagsOnly = New Regex("</?a\b[^>]*>", RegexOptions.IgnoreCase)
For Each m In matches
'tmpText holds the data extracted within the a tags. /visit at.../www.applyhere.com
Dim tmpText = rxgATagsContent.Replace(m.ToString, "")
'if the data extract between the tags looks like a URL then take off the a tags without touching the span tags.
If regURL.IsMatch(tmpText) Then
'select everything but a tags
Dim noATagsStr As String = rxgATagsOnly.Replace(m.ToString, Environment.NewLine)
'replaces string with a tag to non a tag string keeping it's span tags
ret = ret.Replace(m.ToString, noATagsStr)
End If
Next
so 来自字符串:
<a href=www.cnn.com><span style="color: rgb(255, 0, 0);">www.cnn.com</span></a>
我只选择了带有 Avinash Raj 正则表达式的 a 标签然后用"替换它们.谢谢大家的回答.
i selected only the a tags with Avinash Raj regex and then replaced them with "". Thank you all for answering.
这篇关于vb.net Regex - 替换标签而不替换跨度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文