Search code examples
arraysvb.net

VB.NET Regex.Replace not working (array not updating)


It must be a really silly error that I just can't see here... or my recent switch from .NET Framework to .NET8 plays tricks on me. I'm simply trying to get rid of HTML tags in an array of strings, and whilst debugging I see that my variable "l" gets updated properly, i.e. the Regex itself works. But the line isn't updated in the array.

Would someone please point to my error? Thank you ever so much!

Here's my code:

Private Function RemoveHTMLTags(content As String) As String()

    Dim lines() = content.Split(vbLf)
    For Each l In lines
        l = Regex.Replace(l, htmlRegexG, String.Empty)
        'l = ... more changes here
    Next
    Return lines

End Function

EDIT: Just to avoid confusion, htmlRegexG is defined as string "<\w>|</\w>" outside the method and it does change the variable!


Solution

  • You could update line(i) instead of l:

    Private Function RemoveHTMLTags(content As String) As String()
        Dim lines() = content.Split(vbLf)
        For i As Integer = 0 To lines.Length - 1
            lines(i) = Regex.Replace(lines(i), htmlRegexG, String.Empty)
            'lines(i) = ... also modify the other changes
        Next
        Return lines
    End Function