繁体   English   中英

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

[英]C# - Removing a Line that matches a Regex

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

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

我想删除以9 xxx开头的每一行。 现在我已经尝试使用正则表达式替换值。 这就是我所拥有的:

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 

问题:

  • 有没有更简单的方法来 go 关于这个?
  • 如果是这样,我可以使用正则表达式来 go 关于这个还是我必须使用不同的方式?

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

试试这个(使用 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");

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

你为什么不只匹配第一个 9xxx 部分,使用通配符来匹配该行的 rest,它会更具可读性。

output = Regex.Replace(output, @"^9[\d{3}].*", "")

暂无
暂无

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

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