如何使用VB.NET实现像textbox2这样的结果

发布于 2025-01-15 10:04:36 字数 845 浏览 2 评论 0原文

我有一个在 vb.net 中使用的正则表达式,我想将所有符合条件的 textbox1 逐行输出到 textbox2,这就是我正在使用的。

 textbox1(Sorce)                    textbox2(Result)
  123kaadd234                             123,234
fjalj787fafkajl34ddfa999               787,134,999  

我的代码:

Imports System.Text.RegularExpressions
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim pattern As String = "\d{3}"
        For row As Int16 = 0 To TextBox1.Lines.Count - 1
            Dim input As String = TextBox1.Lines(row)
            Dim m As MatchCollection = Regex.Matches(input, pattern)
            For i As Int16 = 0 To m.Count - 1
                TextBox2.Text = TextBox2.Text & m(i).ToString & "," & vbCrLf
            Next
        Next
    End Sub
End Class

I have a regex that I use in vb.net, I want to output all eligible textbox1 line by line to textbox2, this is what I'm using.

 textbox1(Sorce)                    textbox2(Result)
  123kaadd234                             123,234
fjalj787fafkajl34ddfa999               787,134,999  

My Code:

Imports System.Text.RegularExpressions
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim pattern As String = "\d{3}"
        For row As Int16 = 0 To TextBox1.Lines.Count - 1
            Dim input As String = TextBox1.Lines(row)
            Dim m As MatchCollection = Regex.Matches(input, pattern)
            For i As Int16 = 0 To m.Count - 1
                TextBox2.Text = TextBox2.Text & m(i).ToString & "," & vbCrLf
            Next
        Next
    End Sub
End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀お生予夺 2025-01-22 10:04:36

首先,第二行的结果应该是 787,999,因为 34 之前没有 1(有一个字母 l)代码>)。

其余代码可以使用

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim pattern As String = "\d{3}"
    For row As Int16 = 0 To TextBox1.Lines.Count - 1
        Dim input As String = TextBox1.Lines(row)
        Dim line As String = String.Join(",", Regex.Matches(input, pattern).Cast(Of Match)().Select(Function(m) m.Value).ToList())
        TextBox2.AppendText(line & vbCrLf)
    Next
End Sub

注意修复:

  • Regex.Matches(input,pattern).Cast(Of Match)().Select(Function(m) m.Value).ToList() 收集将所有匹配值放入列表中
  • String.Join(",", ...) 将列表连接成单个逗号分隔的字符串
  • TextBox2.AppendText(line & vbCrLf) 将新行附加到多行文本框控件。

First of all, the second line should result in 787,999 as there is no 1 before 34 (there is a letter l).

The rest of the code can be fixed using

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim pattern As String = "\d{3}"
    For row As Int16 = 0 To TextBox1.Lines.Count - 1
        Dim input As String = TextBox1.Lines(row)
        Dim line As String = String.Join(",", Regex.Matches(input, pattern).Cast(Of Match)().Select(Function(m) m.Value).ToList())
        TextBox2.AppendText(line & vbCrLf)
    Next
End Sub

Note:

  • Regex.Matches(input, pattern).Cast(Of Match)().Select(Function(m) m.Value).ToList() collects all match values into a list
  • String.Join(",", ...) joins the list into a single comma-separated string
  • TextBox2.AppendText(line & vbCrLf) appends a new line to the multiline text box control.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文