简体   繁体   English

使用 C# 删除 .srt 文件中的编号

[英]Removing numeration in .srt file using C#

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.我需要删除 .srt 文件中的编号,但我找不到正确的正则表达式,使用正则表达式可能不是一个好主意,因为字幕本身可以包含一个数字,我正在考虑匹配“-->”和然后删除上一行,但我做不到。 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);
   

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() .如果文件不是超级大,更好的方法是读取文件的每一行并使用Int32.TryParse()检查每一行。 If True is returned for that line, skip it, otherwise write the contents of the line to an output file.如果该行返回 True,则跳过它,否则将该行的内容写入 output 文件。

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 "-->".如果活动行不包含“-->”,则添加上一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM