C# - 移除与正则表达式匹配的行

3

我有一些数据...它看起来类似于这样:

0423 222222 ADH, TEXTEXT 
0424 1234 ADH,MORE TEXT 
0425 98765 ADH, TEXT 3609 
2000 98765-4 LBL,IUC,PCA,S/N 
0010 99999-27 LBL,IUI,1.0x.25 
9000 12345678 HERE IS MORE, TEXT
9010 123-123 SOMEMORE,TEXT1231
9100 SD178 YAYFOR, TEXT01
9999 90123 HEY:HOW-TO DOTHIS

我希望能够删除每一行开头是9xxx整行内容。目前我尝试使用正则表达式进行替换,以下是我的代码:

output = Regex.Replace(output, @"^9[\d]{3}\s+[\d*\-*\w*]+\s+[\d*\w*\-*\,*\:*\;*\.*\d*\w*]+", "");

然而,这段文本真的很难读懂,并且它实际上并没有删除整行。


代码: 以下是我正在使用的代码部分:

        try
        {
            // Resets the formattedTextRichTextBox so multiple files aren't loaded on top of eachother.
            formattedTextRichTextBox.ResetText();

            foreach (string line in File.ReadAllLines(openFile.FileName))
            {
                // Uses regular expressions to find a line that has, digit(s), space(s), digit(s) + letter(s),
                // space(s), digit(s), space(s), any character (up to 25 times).
                Match theMatch = Regex.Match(line, @"^[\.*\d]+\s+[\d\w]+\s+[\d\-\w*]+\s+.{25}");

                if (theMatch.Success)
                {
                    // Stores the matched value in string output.
                    string output = theMatch.Value;

                    // Replaces the text with the required layout.
                    output = Regex.Replace(output, @"^[\.*\d]+\s+", "");
                    //output = Regex.Replace(output, @"^9[\d]{3}\s+[\d*\-*\w*]+\s+[\d*\w*\-*\,*\:*\;*\.*\d*\w*]+", "");
                    output = Regex.Replace(output, @"\s+", " ");

                    // Sets the formattedTextRichTextBox to the string output.
                    formattedTextRichTextBox.AppendText(output);
                    formattedTextRichTextBox.AppendText("\n");
                }
            }
        }

结果: 我希望新数据的格式如下(去除9xxx):

0423 222222 ADH, TEXTEXT 
0424 1234 ADH,MORE TEXT 
0425 98765 ADH, TEXT 3609 
2000 98765-4 LBL,IUC,PCA,S/N 
0010 99999-27 LBL,IUI,1.0x.25 

问题:

  • 有没有更简单的方法来解决这个问题?
  • 如果有,我可以使用正则表达式来解决这个问题吗?还是必须使用其他方法?
4个回答

2

试试这个(使用 Linq):

//Create a regex to identify lines that start with 9XXX
Regex rgx = new Regex(@"^9\d{3}");
//Below is the linq expression to filter the lines that start with 9XXX
var validLines = 
(
//This following line specifies what enumeration to pick the data from 
from ln in File.ReadAllLines(openFile.FileName)
//This following specifies what is the filter that needs to be applied to select the data. 
where !rgx.IsMatch(ln)
//This following specifies what to select from the filtered data.
select ln;
).ToArray(); //This line makes the IQueryable enumeration to an array of Strings (since variable ln in the above expression is a String)
//Finally join the filtered entries with a \n using String.Join and then append it to the textbox
formattedTextRichTextBox.AppendText = String.Join(validLines, "\n");

好的建议。根据问题和示例代码,我认为正则表达式应该是 @"^9\d{3}",这样它就可以匹配一个后面跟着三个数字的 9。 - John M Gant
@John:谢谢。已更新帖子,使正则表达式 less restrictive(更不限制)。 - Chandu
@Cybernate:我不确定这段代码中发生了什么。你能解释一下吗? - theNoobGuy
@Cybernate:好的,现在这个更有意义了。不过,有没有办法从RichTextBox中读取而不是从文件中读取呢? - theNoobGuy
@Cybernate:没事了,我已经解决了!感谢你的帮助——特别是关于LINQ的部分! :) - theNoobGuy
显示剩余2条评论

2

只需重新组合测试格式的正则表达式,以匹配不以9开头的所有内容 - 这样以9开头的行就不会被添加到富文本框中。


你知道我该怎么做吗?我如何排除? - theNoobGuy
基本上,只需将表达式 @"^[\.*\d]+\s+[\d\w]+\s+[\d\-\w*]+\s+.{25}" 替换为 ^[0-8][\d]{3},这样以9开头的行就不再匹配。 - Thomas Gerstendörfer

1

是的,有更简单的方法。只需使用 Regex.Replace 方法,并提供 Multiline 选项。


1
为什么不直接匹配前面的9xxx部分,然后使用通配符匹配行的其余部分,这样会更易读。 output = Regex.Replace(output, @"^9[\d{3}].*", "")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接