正则表达式在文本框而不是消息框中

Jef*_*eff 1 regex vb.net

我需要将我提取的文本(使用正则表达式)放在a中TextBox,而不是a中MessageBox.

这是我目前的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim source As String
    Using wc As New WebClient()
        source = wc.DownloadString("http://www.twstats.com/en71/index.php?page=rankings&mode=players")
    End Using

    Dim mcol As MatchCollection = Regex.Matches(source, "page=player&amp;id=\d+"">(.+)</a>")
    For Each m As Match In mcol
        MessageBox.Show(m.Groups(1).Value)
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)

现在我需要添加显示MessageBox在a中的文本TextBox.

我怎样才能做到这一点?

编辑:

如果我TextBox在循环中使用a 而不是MessageBox它只显示最后提取的值.

pas*_*sty 5

您需要将中间字符串保存到变量中.当一个字符串相互添加时,一个很好的选择是.NET提供的StringBuilder类.调用该操作string concatenation- 它可用于动态扩展具有新内容的相同字符串.

可能的解决方案可能如下所示:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim source As String
    Using wc As New WebClient()
        source = wc.DownloadString("http://www.twstats.com/en71/index.php?page=rankings&mode=players")
    End Using
    ' save temporarily the different strings
    Dim sb as StringBuilder = new StringBuilder()
    'alternative
    'Dim output as String = String.Empty;
    Dim mcol As MatchCollection = Regex.Matches(source, "page=player&amp;id=\d+"">(.+)</a>")
    For Each m As Match In mcol
        'MessageBox.Show(m.Groups(1).Value)
        ' add every line to the "output"
        sb.AppendLine(m.Groups(1).Value)
        'output = output + Environment.NewLine + m.Groups(1).Value
    Next
    ' show the output = all lines
    textBox.Text = sb.ToString()
    'textBox.Text = output
End Sub
Run Code Online (Sandbox Code Playgroud)

textBox使用您的变量名重命名.它也可以是一个RichTextbox控件.我还添加了第二个变体,仅使用字符串变量来实现所需的结果.您可以选择其中一个实现.