我有一种情况,我需要从一些文本中删除HTML代码。但是,有些输入文本包含列表,在这种情况下,我希望保留编号。
如果我做了
result = Regex.Replace(result, "<li>", vbNewLine & "1. ", RegexOptions.IgnoreCase)然后,在去掉其他HTML标记之后,我的结果是:
1. List item one
1. List item two
1. List item three有什么方法可以在替换时得到比赛的索引吗?
例如:
result = Regex.Replace(result, "<li>", vbNewLine & replacementIndex + 1 & " ", RegexOptions.IgnoreCase)然后,在去掉其他HTML标记之后,我会得到:
1. List item one
2. List item two
3. List item three这个是可能的吗??
注意:这是在一个函数中,因此每个列表都是分开处理的,无序列表则得到符号(*)。
发布于 2018-08-07 23:27:06
下面是我最后是如何做到的--首先,找到每个订单列表:
Dim result As String = rawText
Dim orderedLists As MatchCollection = Regex.Matches(rawText, "<ol>.*?</ol>", RegexOptions.Singleline)
For Each ol As Match In orderedLists
result = Replace(result, ol.Value, EncodeOrderedList(ol.Value))
Next以及转换每个函数的函数:
Private Function EncodeOrderedList(ByVal rawText As String) As String
Dim result As String = rawText
result = Regex.Replace(result, "<ol>\s*<li>", "1. ", RegexOptions.IgnoreCase)
result = Regex.Replace(result, "</li>\s*</ol>", vbNewLine & vbNewLine, RegexOptions.IgnoreCase)
Dim bullets As MatchCollection = Regex.Matches(rawText, "</li>\s*<li>")
Dim i As Integer = 2
For Each li As Match In bullets
result = Replace(result, li.Value, vbNewLine & i & ". ", 1, 1)
i += 1
Next
Return result
End Function我还没有在嵌套列表上测试过它。
发布于 2016-06-24 22:23:59
这应该是一个很好的起点。@"(\<ul\>)((.|\n)*?)(\<\/ul\>)" --这将匹配标记之间的所有内容。
发布于 2016-06-24 23:03:34
很乱,但有点像下面这样。一次只换一个。对于大型数据集来说,这可能比较慢。
int lineNbr = 1;
string newResult = result.Replace("(?i)<li>", vbNewLine & (lineNbr++).ToString() & '. ', 1);
while (newResult != result)
{
result = newResult;
newResult = result.Replace("(?i)<li>", vbNewLine & (lineNbr++).ToString() & '. ', 1);
}https://stackoverflow.com/questions/38022830
复制相似问题