如何在字符串中查找字符串不包含标记?

时间:2016-03-14 05:01:22

标签: .net vb.net

 Dim strTextTag="Terminology <VN_CR NO="1"/>for test in the glossary. Terminology <VN_F NO="1"><VN_CR NO="2"/></VN_F>for test"
 Dim strText="Terminology for test in the glossary. Terminology for test"
 Dim strValue="Terminology for test"

我如何首先: 在搜索之前删除[<VN_CR NO="1"/>]和[<VN_F NO="1"><VN_CR NO="2"/></VN_F>]。

如果strValue中存在strTextTag,我如何获得strValue中所有strText的排名?

1 个答案:

答案 0 :(得分:-1)

您可以使用RegEx.Replace查找并删除所有代码。并使用Regex.Matches函数查找另一个字符串中出现的所有字符串。

Function RemoveTags(ByVal textWithTags As String) As String
    Dim result As String = Regex.Replace(textWithTags, "<[^>]+?>", " ") '-- remove the tags
    result = result.Replace("  ", " ")      '-- remove any extra whitespaces that might have got in due to regex replacement
    Return result
End Function

Function GetTextPositions(ByVal text As String, ByVal find As String) As Integer()
    Dim result As New List(Of Integer)
    Dim re As New Regex(Regex.Escape(find))
    For Each match As Match In re.Matches(text)
        result.Add(match.Index)
    Next
    Return result.ToArray
End Function