开发者

C# - Removing a Line that matches a Regex

开发者 https://www.devze.com 2023-03-21 16:54 出处:网络
I have some data.. it looks similar to this: 0423 222222 ADH, TEXTEXT 0424 1234 ADH,MORE TEXT 0425 98765 ADH, TEXT 3609

I have some data.. it looks similar to this:

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

And I would like to remove each entire line that begins with a 9xxx. Right now I have tried Replacing the value 开发者_运维技巧using Regex. Here is what I have for that:

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

However, this is really hard to read and it actually does not delete the entire line.


CODE: Here is the section of the code I am using:

        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");
                }
            }
        }

OUTCOME: So what I would like the new data to look like is in this format (removed 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 

QUESTIONS:

  • Is there an easier way to go about this?
  • If so, can I use regex to go about this or must I use a different way?


Just reformulate the regex that tests your format to match everything that doesn't begin with 9 - that way lines starting with 9 are not added to the rich text box.


Try this(Uses 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");


Yes, there is a simpler way. Just use Regex.Replace method, and provide Multiline option.


Why don't you just match the first 9xxx part the use a wildcard to match the rest of the line, it would be a lot more readable.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消