I created a list from a subtitle file (.srt) in which every line is inside a index of the list. Now I want to erase some lines, specially lines starting with the time interval in the format “xx:xx:xx,xxx –> xx:xx:xx,xxx”. I did some research (ok, quite superficial research on a complex topic, I might add) and tried to create the following sub:
Private Shared Sub listCleaning(ByRef sList As List(Of String))
For Each line As String In sList
Dim pattern As String = "\b\d\d:\d\d:\d\d:\d\d,\d\d\d --> \b\d\d:\d\d:\d\d:\d\d,\d\d\d"
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
Now Im having two problems:
- My Regular Expression is not beeing correctly parsed.
- When I have a valid condition on my If Block and the line gets removed, I have a InvalidOperationException when hiting next since the list has changed.
Can anyone help me to write a correct RegEx and a way to iterate through the list removing the unwanted lines?
tnx in advance.
EDIT:
Ok, Tim got most of the problem out but I still need a RegEx that suits the pattern “xx:xx:xx,xxx –> xx:xx:xx,xxx”. Anyone willing to care?
thanks in advance!
These are my changes to your original code.
First, I don’t use the for each loop, but the more traditional for loop
Second, Loop in reverse, so your delete goes to the end of the list
Third, the regex pattern should be outside the loop
This is my test data:
To get this output