Visual Basic problem with Trimming values corectly

46 Views Asked by At

I’m making a program to crawl a webpage source and to display it in a datagridview but it fails to display the result correctly because of some inconsistency and it does not end displaying it on the correct place either.

What I have:

Private Function ExtractSide(sourceCode As String) As String
    Dim pattern As String = "Side: (.*?) [\/li]"
    Dim match As Match = Regex.Match(sourceCode, pattern)

    If match.Success Then
        Return match.Groups(1).Value
    Else
        Return String.Empty
    End If
End Function

The text strings in the source are one of the following

Side: Both[/li]
Side: None[/li]
Side: [span class=icon-alliance]Alliance[/span][/li]
Side: [span class=icon-horde]Horde[/span][/li]

I only want to be left with Both, None, Alliance or Horde.

1

There are 1 best solutions below

2
Darryl On BEST ANSWER

Try Dim pattern = "Side: (\[[^]]*])?(.*?)(\[[^]]*])", then return group 2 instead of group 1:

Public Shared Function ExtractSide(sourceCode As String) As String
    Dim pattern = "Side: (\[[^]]*])?(.*?)(\[[^]]*])"
    Dim match As Match = Regex.Match(sourceCode, pattern)

    If match.Success Then
        Return match.Groups(2).Value
    Else
        Return String.Empty
    End If
End Function