I am an amateur in coding and I'm struggling with a simple problem. I need to remove the numeration in the .srt file, but I couldn't find the correct regex, using regex might be a bad idea, because the subtitle itself can contain a number, I was thinking about matching "-->" and then removing the previous line, but I couldn't do it. Can someone help me?
I need to remove the following lines (numeration) screenshot
UPDATE
I came up with the following code, which replaces numbers with spaces, but it doesn't delete the line, as I want
var source = File.ReadAllLines("C:\\Users\\name\\Desktop\\temp1\\Doctor.Strange.2016.720p.BluRay.x264.[YTS.MX]-English.srt");
var newFile = @"C:\Users\name\Desktop\temp1\new.srt";
for (int i = 0; i < source.Length; i )
{
if (source[i].Contains(matchSymbol))
{
source[i - 1] = "";
}
}
File.WriteAllLines(newFile, source);
CodePudding user response:
If the file is not super large, a better way would be to read every line of the file and check each line with Int32.TryParse(). If True is returned for that line, skip it, otherwise write the contents of the line to an output file.
CodePudding user response:
Something like this should work:
void RemoveNumberLines()
{
var source = File.ReadAllLines("C:\\Users\\name\\Desktop\\temp1\\Doctor.Strange.2016.720p.BluRay.x264.[YTS.MX]-English.srt");
var newFile = @"C:\Users\name\Desktop\temp1\new.srt";
var matchSymbol = "-->";
var myNewFile = new List<string>(); bool addPrevious = true; string previous = "";
foreach (var item in source)
{
addPrevious = !item.Contains(matchSymbol);
if (addPrevious)
{
myNewFile.Add(previous);
}
previous = item;
}
if (addPrevious)
{
myNewFile.Add(previous);
}
File.WriteAllLines(newFile, myNewFile);
}
Adding the previous line, if the active line won't contain "-->".