0

So basically what I am trying to do in vb.net is remove multiple nested parentheses and all the text inside those parentheses from a string. It's easy to do if there is just one set of parentheses like in the first example below I just find the index of "(" and ")" and then use the str.remove(firstindex, lastindex) and just keep looping until all parentheses have been removed from the string.

str = "This (3) is(fasdf) an (asdas) Example"

Desired output:
str = "This is an example"

However I still can't figure out how to do it if their are multiple nested parentheses in the string.

str = "This ((dsd)sdasd) is ((sd)) an (((d))) an example"

Desired Outcome:
str = "This is an example"
teckuser55
  • 27
  • 6

1 Answers1

1

This isn't really a tutorial site, so I shouldn't be answering this, but I couldn't resist.

As Ahmed said, you could use Regex.Replace, but I find Regexes complex and impenetrable. So it would be difficult for someone else to maintain it.

The following code has three loops. The our loop, a While loop, will run the two inner loops as long as the character index is less than the length of the string.

The first inner loop searches for the first "open bracket" in a group and records the position and adds 1 to the number of "open brackets" (the depth). Any subsequent "open brackets" just adds 1 to the number of brackets. This carries on until the first loop finds a "close bracket"

Then the second loop searches for the same number of "close brackets" from that point where the first "close bracket" is found.

When the loop gets to the last "close bracket" in the group, all the characters from the first "open bracket" to the last "close bracket" in the group are removed. Then the While loop starts again if the current index position is not at the end of the updated inputString.

When the While loop finishes, any double spaces are removed and the updated output string is returned from the function

Private Function RemoveBracketsAntContents(inputString As String) As String
    Dim i As Integer
    While i < inputString.Length
        Dim bracketDepth As Integer = 0
        Dim firstBracketIndex As Integer = 0
        Do
            If inputString(i) = "(" Then
                If firstBracketIndex = 0 Then
                    firstBracketIndex = i
                End If
                bracketDepth += 1
            End If
            i += 1
        Loop Until i = inputString.Length OrElse inputString(i) = ")"
        If i = inputString.Length Then Exit While
        Do
            If inputString(i) = ")" Then
                bracketDepth -= 1
            End If
            i += 1
        Loop Until bracketDepth = 0
        inputString = inputString.Remove(firstBracketIndex, i - firstBracketIndex)
        i = i - (i - firstBracketIndex)
    End While
    inputString = inputString.Replace("  ", " ")
    Return inputString
End Function
David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • Thanks for all the responses I was able to solve my problem using Regex.Replace and an regex expression generator online. – teckuser55 Mar 28 '18 at 14:00