如何使用VB.NET实现像textbox2这样的结果
我有一个在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,第二行的结果应该是
787,999
,因为34
之前没有1
(有一个字母l
)代码>)。其余代码可以使用
注意修复:
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 no1
before34
(there is a letterl
).The rest of the code can be fixed using
Note:
Regex.Matches(input, pattern).Cast(Of Match)().Select(Function(m) m.Value).ToList()
collects all match values into a listString.Join(",", ...)
joins the list into a single comma-separated stringTextBox2.AppendText(line & vbCrLf)
appends a new line to the multiline text box control.