C# - Parse/Format a .txt file
So I have some .txt files that have been formatted in a way that I do not like. I want to read the file and reformat it by a click of a button (or 2) in my GUI. Also, I would like to be able resave the file with many options with a click of another button. Also, if it is possible I would like to have the original file be displayed in a rich text box on the left side of my GUI, and once the format button is clicked it will display the new text on the right side of my GUI in a seperate rich text box.
So I currently have a functioning "Open File" button, "Save File" button, and "Clear Text" button. However, I need a "Format Text" button (unless we can combine the open file button and the format text button into just one button!)...
Here is what the file will 开发者_开发知识库look like when it comes in. http://i.stack.imgur.com/mlSMm.png
And this is what I want it to look like when I click format. http://i.stack.imgur.com/1IzKF.png
I also have a GUI that I have made and to open and save the file I have the following code:
private void openFileButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.DefaultExt = "*.txt";
openFile.Filter = ".txt Files|*.txt";
openFile.InitialDirectory = "C:\\";
openFile.RestoreDirectory = true;
try
{
if(openFile.ShowDialog() == DialogResult.OK && openFile.FileName.Length > 0)
{
openedTextRichTextBox.LoadFile(openFile.FileName, RichTextBoxStreamType.PlainText);
}
else
throw new FileNotFoundException();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void saveFileButton_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.DefaultExt = "*.txt";
saveFile.Filter = ".txt Files|*.txt";
saveFile.InitialDirectory = "C:\\";
saveFile.RestoreDirectory = true;
try
{
if(saveFile.ShowDialog() == DialogResult.OK && saveFile.FileName.Length > 0)
{
formattedTextRichTextBox.LoadFile(saveFile.FileName, RichTextBoxStreamType.PlainText);
}
else
throw new FileNotFoundException();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Okay so the actual question is:
How do I format the incoming txt file to delete everything except for (not including) the columns labeled "level", "number ref", "component item", "description". This meaning, everything under the "---" until I hit another "---". After I hit another "---" I need to grab the same columns as above. Does this make more sense? The example of how I want it to look is in the second link.
Run the text through a regular expression that picks out the lines of interest Something along these lines:
foreach (string line in File.ReadAllLines("filename"))
{
Match m = Regex.Match(line, @"^\d+\s+[\d\w]+\s+\d+\s+.{24}");
if (m.Success)
{
string output = m.Value;
// do something with output, for example write to a file
}
}
If you are not familiar with regular expressions, you should look into them, for example here: http://www.regular-expressions.info/
精彩评论