且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式:在行尾之前匹配文本

更新时间:2023-02-26 13:13:55

我认为您正在使它变得比实际更复杂.例如,以下内容可以帮助您删除数据的最后部分(如果按照示例进行格式化),并进行一些微调,例如修整(并且显然可以减少错误),我敢肯定这很适合:

I think that you're making this more complex than it really is; for instance, the following should help you removing the last part of the data if formatted as per your example, with a little tweaking, such as trimming (and, obviously, error mitigation), I'm sure this would suit:

var lines = new List<string>(File.ReadAllLines(path));
for (int i = 0; i < lines.Count; i++) 
{
    var idx = lines[i].LastIndexOf(" ");   
    if (idx != -1)
    {     
        lines[i] = lines[i].Remove(idx);
    }
}

请注意,可以一口气读取文件的所有行,根据要加载的文件的大小,并不总是需要这样做,但是我看到您无论如何都在加载每行处理-在这种情况下,我们可以使整个过程更加简洁.

Note that it is possible to read all lines of a file in one fell swoop, this isn't always desired depending on the size of the file to be loaded, but I see you're loading each of the lines anyway before processing - in which case we can just make the whole thing more concise.