Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 2.55 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Problems With Regular Expressions and Lists in vb.net
  2. Private Shared Sub listCleaning(ByRef sList As List(Of String))
  3.     For Each line As String In sList
  4.         Dim pattern As String = "bdd:dd:dd:dd,ddd --> bdd:dd:dd:dd,ddd"
  5.         Dim reg As New Regex(pattern)
  6.  
  7.         If line = "" Or Integer.TryParse(line, Nothing) Or reg.IsMatch(pattern) Then
  8.             sList.Remove(line)
  9.         End If
  10.     Next
  11. End Sub
  12.        
  13. Private Shared Sub listCleaning(ByRef sList As List(Of String))
  14.     Dim pattern As String = "d{2}:d{2}:d{2},d{3}s+-->s+d{2}:d{2}:d{2},d{3}"
  15.     Dim reg As New Regex(pattern)
  16.     Dim x as Integer
  17.     For x = sList.Count - 1 to 0 step -1
  18.         Dim line as string = sList(x)
  19.                 Console.WriteLine(line)
  20.         If line = "" Or Integer.TryParse(line, Nothing) Or reg.IsMatch(line) Then
  21.                sList.Remove(line)
  22.         End If
  23.     Next
  24. End Sub
  25.        
  26. Sub Main
  27.     Dim sList as List(Of String) = new List(Of string)
  28.     sList.Add("01:01:01,003 --> 02:02:02,003")
  29.     sList.Add("sdsdfsdfsd03 --> 02:02:02,003")
  30.     sList.Add("03:01:01,003 --> 03:02:02,003")
  31.     sList.Add("04:01:01,003 --> 04:02:02,003")
  32.     sList.Add("05:01:01,003 --> 05:02:02,003")
  33.     sList.Add("06:01:01,003 --> 06:02:02,003")
  34.     sList.Add("07:01:01,003 --> 07:02:02,003")
  35.     sList.Add("08:01:01,003 --> 08:02:02,003")
  36.     sList.Add("09:01:01,003 --> 02:02:02 003")    
  37.  
  38.     console.WriteLine("Call listCleaning with " + sList.Count.ToString +  " elements")
  39.  
  40.     listCleaning(sList)
  41.  
  42.     console.WriteLine("Returned with " + sList.Count.ToString +  " elements")
  43.     for each line as String in sList
  44.         Console.WriteLine(line)
  45.     next
  46. End Sub
  47.        
  48. Call listCleaning with 9 elements
  49. 09:01:01,003 --> 02:02:02 003
  50. 08:01:01,003 --> 02:02:02,003
  51. 07:01:01,003 --> 02:02:02,003
  52. 06:AA:01,003   --> 02:02:02,003
  53. 05:01:01,003 -->    02:02:02,003
  54. 04:01:01,003 --> 02:02:02,003
  55. 03:01:01,003 --> 02:02:02,003
  56. sdsdfsdfsd03 --> 02:02:02,003
  57. 01:01:01,003 --> 02:02:02,003
  58. Returned with 3 elements
  59. sdsdfsdfsd03 --> 02:02:02,003
  60. 06:AA:01,003   --> 02:02:02,003
  61. 09:01:01,003 --> 02:02:02 003
  62.        
  63. var dontRemove = From line In sList
  64.     Where line <> "" AndAlso Not Integer.TryParse(line, Nothing) AndAlso Not reg.IsMatch(pattern)
  65.        
  66. sList = dontRemove.ToList()
  67.        
  68. Dim regex = New Regex("d{2}:d{2}:d{2},d{3}s+-->s+d{2}:d{2}:d{2},d{3}", RegexOptions.Compiled)
  69. sList.RemoveAll(Function(line) line.Length = 0 _
  70.                     OrElse Integer.TryParse(line, Nothing) _
  71.                     OrElse Not regex.IsMatch(line))
  72.        
  73. For index As Int32 = sLines.Count - 1 To 0 Step -1
  74.     Dim line = sLines(index)
  75. Next`