读取一行到列表框的多个整数

时间:2015-04-27 15:32:38

标签: vb.net visual-studio-2010 visual-studio visual-studio-2012

我试图读取列表框行中的所有整数。

Dim scores As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(line, "\d+")

我已经以这样的格式保存了分数

  

姓名00 00 00

我可以使用正则表达式来读取每个数字之间有空格的3个整数吗?存储整数的文件是记事本文件。

2 个答案:

答案 0 :(得分:1)

没有正则表达式的方法:

Const SEPARATOR As String = " "c
Dim line As String 'Here reading line from the file
Dim numbers As New List(Of Int32)()
Dim values As String() = line.Split(SEPARATOR) 'Split values to array
For Each value As String in values
    Dim tempnumber As Int32
    If Int32.TryParse(value, tempnumber) = True Then
        'Accept only numbers
        numbers.Add(tempnumber)
    End If
Next

答案 1 :(得分:1)

使用Matches代替Match并将Regex的结果存储到MatchCollection

Sub Main()
    Dim scores As String = "00 13 00"
    Dim score As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(scores, "\d+")

    For i As Integer = 0 To score.Count - 1
        Console.WriteLine(score.Item(i))
    Next

    Console.ReadLine()
End Sub

结果:

enter image description here