这些是我对您的原始代码所做的更改。
首先,我不使用 for each 循环,而是更传统的 for 循环
第二,反向循环,所以你的删除到列表的末尾
第三,正则表达式模式应该在循环之外
Private Shared Sub listCleaning(ByRef sList As List(Of String))
Dim pattern As String = "\d{2}:\d{2}:\d{2},\d{3}\s+-->\s+\d{2}:\d{2}:\d{2},\d{3}"
Dim reg As New Regex(pattern)
Dim x as Integer
For x = sList.Count - 1 to 0 step -1
Dim line as string = sList(x)
Console.WriteLine(line)
If line = "" Or Integer.TryParse(line, Nothing) Or reg.IsMatch(line) Then
sList.Remove(line)
End If
Next
End Sub
这是我的测试数据:
Sub Main
Dim sList as List(Of String) = new List(Of string)
sList.Add("01:01:01,003 --> 02:02:02,003")
sList.Add("sdsdfsdfsd03 --> 02:02:02,003")
sList.Add("03:01:01,003 --> 03:02:02,003")
sList.Add("04:01:01,003 --> 04:02:02,003")
sList.Add("05:01:01,003 --> 05:02:02,003")
sList.Add("06:01:01,003 --> 06:02:02,003")
sList.Add("07:01:01,003 --> 07:02:02,003")
sList.Add("08:01:01,003 --> 08:02:02,003")
sList.Add("09:01:01,003 --> 02:02:02 003")
console.WriteLine("Call listCleaning with " + sList.Count.ToString + " elements")
listCleaning(sList)
console.WriteLine("Returned with " + sList.Count.ToString + " elements")
for each line as String in sList
Console.WriteLine(line)
next
End Sub
得到这个输出
Call listCleaning with 9 elements
09:01:01,003 --> 02:02:02 003
08:01:01,003 --> 02:02:02,003
07:01:01,003 --> 02:02:02,003
06:AA:01,003 --> 02:02:02,003
05:01:01,003 --> 02:02:02,003
04:01:01,003 --> 02:02:02,003
03:01:01,003 --> 02:02:02,003
sdsdfsdfsd03 --> 02:02:02,003
01:01:01,003 --> 02:02:02,003
Returned with 3 elements
sdsdfsdfsd03 --> 02:02:02,003
06:AA:01,003 --> 02:02:02,003
09:01:01,003 --> 02:02:02 003