- Problems With Regular Expressions and Lists in vb.net
- Private Shared Sub listCleaning(ByRef sList As List(Of String))
- For Each line As String In sList
- Dim pattern As String = "bdd:dd:dd:dd,ddd --> bdd:dd:dd:dd,ddd"
- Dim reg As New Regex(pattern)
- If line = "" Or Integer.TryParse(line, Nothing) Or reg.IsMatch(pattern) Then
- sList.Remove(line)
- End If
- Next
- End Sub
- 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
- var dontRemove = From line In sList
- Where line <> "" AndAlso Not Integer.TryParse(line, Nothing) AndAlso Not reg.IsMatch(pattern)
- sList = dontRemove.ToList()
- Dim regex = New Regex("d{2}:d{2}:d{2},d{3}s+-->s+d{2}:d{2}:d{2},d{3}", RegexOptions.Compiled)
- sList.RemoveAll(Function(line) line.Length = 0 _
- OrElse Integer.TryParse(line, Nothing) _
- OrElse Not regex.IsMatch(line))
- For index As Int32 = sLines.Count - 1 To 0 Step -1
- Dim line = sLines(index)
- Next`