正则表达式:在行尾之前匹配文本 [英] Regex: Match the text before the end of line

查看:276
本文介绍了正则表达式:在行尾之前匹配文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的文件:

I have a file that looks like this:

J6      INT-00113G  227.905    5.994  180  ~!@#$%&^)
J3      INT-00113G  227.905 -203.244  180  12341341312315
U13     EXCLUDES    -42.210  181.294  180  QFP128
U3      IC-00276G     5.135  198.644  90   B%GA!@-48
U12     IC-00270G  -123.610 -201.594  0    SOP8_000
J1      INT-00112G  269.665  179.894  180  SOIC16_1
J2      INT-00112G  269.665  198.144  180  SOIC16-_2
..      ..........  .......  .......  ...  ................

我想匹配 第6列 中的最终值,以便将其从列表中删除.第六列中值的长度不确定,可以包含任何字符.所以我想做的是在 space 之前匹配结束值.或只是行尾.

And I would like to match the end value in the 6th column in order to remove it from a list. The length of the value in the 6th column is undetermined and can contain any character. So what I would like to do is match the end value before a space. or just the end of the line.

代码:

CODE:

        // Reads the lines in the file to format.
        var fileReader = File.OpenText(filePath + "\\Remove Package 1 Endings.txt");

        // Creates a list for the lines to be stored in.
        var fileList = new List<string>();

        // Adds each line in the file to the list.
        while (true)
        {
            var line = fileReader.ReadLine();
            if (line == null)
                break;

            fileList.Add(line);
        }

        var mainResult = new List<string>();
        var theResult = new List<string>();

        foreach (var mainLine in fileList)
            mainResult.Add(string.Join(" ", mainLine));

        foreach (var theLine in mainResult)
        {
            // PLACEMENT ONE Regex
            Match theRegex = Regex.Match(theLine, @"insert the regex here!");

            if (theRegex.Success)
                theResult.Add(string.Join(" ", theLine));
        }

        // Removes the matched values from both of the Regex used above.
        List<string> userResult = mainResult.Except(theResult).ToList();

        // Prints the proper values into the assigned RichTextBoxes.
        foreach (var line in userResult)
            richTextBox2.AppendText(line + "\n");

我要执行的操作是使文件看起来像这样:

J6      INT-00113G  227.905    5.994  180
J3      INT-00113G  227.905 -203.244  180
U13     EXCLUDES    -42.210  181.294  180
U3      IC-00276G     5.135  198.644  90
U12     IC-00270G  -123.610 -201.594  0
J1      INT-00112G  269.665  179.894  180
J2      INT-00112G  269.665  198.144  180


问题:


QUESTION:

  • 有人可以为此提供正则表达式吗?

添加的代码:

        var lines = new List<string>(File.ReadAllLines(filePath + "\\Remove Package 1 Endings.txt"));
        for (int i = 0; i < lines.Count; i++)
        {
            var idx = lines[i].LastIndexOf(" ");

            if (idx != -1)
                lines[i] = lines[i].Remove(idx);

            richTextBox1.AppendText(lines[i] + Environment.NewLine
        }

推荐答案

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

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.

这篇关于正则表达式:在行尾之前匹配文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆