如何在字符串中查找字符串不包括标签


 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存在于strText中,我如何获得strTextTag中所有strValue的位置?

您可以使用

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

最新更新