Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-解析/格式化.txt文件_C#_Parsing_File_Format_Text Files - Fatal编程技术网

C#-解析/格式化.txt文件

C#-解析/格式化.txt文件,c#,parsing,file,format,text-files,C#,Parsing,File,Format,Text Files,所以我有一些.txt文件,它们的格式是我不喜欢的。我想通过点击GUI中的按钮(或2)来读取文件并重新格式化它。此外,我想能够重新保存与其他按钮点击许多选项的文件。此外,如果可能的话,我希望原始文件显示在GUI左侧的富文本框中,一旦单击“格式”按钮,它将在GUI右侧的单独富文本框中显示新文本 所以我现在有一个功能正常的“打开文件”按钮,“保存文件”按钮和“明文”按钮。但是,我需要一个“格式化文本”按钮(除非我们可以将“打开文件”按钮和“格式化文本”按钮组合成一个按钮!) 下面是文件传入时的外观。

所以我有一些.txt文件,它们的格式是我不喜欢的。我想通过点击GUI中的按钮(或2)来读取文件并重新格式化它。此外,我想能够重新保存与其他按钮点击许多选项的文件。此外,如果可能的话,我希望原始文件显示在GUI左侧的富文本框中,一旦单击“格式”按钮,它将在GUI右侧的单独富文本框中显示新文本

所以我现在有一个功能正常的“打开文件”按钮,“保存文件”按钮和“明文”按钮。但是,我需要一个“格式化文本”按钮(除非我们可以将“打开文件”按钮和“格式化文本”按钮组合成一个按钮!)

下面是文件传入时的外观。

这就是我希望它在单击“格式”时的样子。

我还制作了一个GUI,要打开并保存文件,我有以下代码:

    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);
       }
    }
好的,那么实际的问题是:


如何格式化传入的txt文件以删除除(不包括)标记为“级别”、“编号参考”、“组件项”、“说明”的列之外的所有内容。这意味着,一切都在“--”下,直到我打到另一个“--”。在我点击另一个“--”之后,我需要抓取与上面相同的列。这更有意义吗?第二个链接中有我希望它看起来如何的示例。

通过一个正则表达式来运行文本,该正则表达式沿着以下几行选择感兴趣的行:

        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
            }
        }

如果您不熟悉正则表达式,您应该研究它们,例如:这里:

您的问题是什么?在保存文件事件中不应该是“formattedTextRichTextBox.SaveFile”吗?GUI与格式转换无关。在不考虑gui的情况下进行格式解析和转换。您能更具体一些吗?“文本格式”是什么意思?您计划如何格式化它?我不清楚你在问什么。@Phil Gan我相信这是一个“让我看看codez”的问题@Colton你能告诉我们到目前为止你在解析文本方面做了什么,或者你在解析文本方面遇到了什么困难吗?Stack Overflow通常不喜欢“我需要做这件事,提供代码来做这件事”这样的问题,这个网站是为“我需要做这件事,我被困在这一步”而设计的,谢谢fredw的帮助!但有一个问题。为什么只打印最后一行而不是每一行?你的代码是什么样子的?是否重写“output”变量并在循环后使用它?foreach(File.ReadAllLines(openFile.FileName)中的字符串行){Match-theMatch=Regex.Match(行,@“^\d+\s+[\d\w]+\s+\d+\s+.{25}”);if(theMatch.Success){string output=theMatch.Value;formattedTextRichTextBox.Text=output;}}您正在覆盖循环中的文本框。请尝试以下操作:string output=“”;foreach(File.ReadAllLines(openFile.FileName)中的字符串行){Match theMatch=Regex.Match(行,@“^\d+\s+[\d\w]+\s+\d+\s+.{25}”);if(theMatch.Success){output+=theMatch.Value+“\n”}}formattedTextRichTextBox.Text=output;嘿,谢谢你的帮助!我发现我没有追加字符串。我把它改为“formattedTextRichTextBox.Append(output);formattedTextRichTextBox.AppendText(“\n”)而且很有效!谢谢你的帮助:)