【问题标题】:Get a specific word from a line read from a .txt从从 .txt 读取的行中获取特定单词
【发布时间】:2016-04-13 17:27:52
【问题描述】:

现在我正在阅读 .txt 文件中的一些行。 比方说,用户输入他的姓名并在 .txt 中保存“在 2016 年 13 月 4 日上午 10:55 登录 {username}”。 (只是一个例子。)

现在我想阅读 .txt 并将特定部分仅打印到文本框中。 意思是,在文本框中应出现“{Username} - 13/04/2016 - 10:55 am”。

到目前为止,我能够从 .txt 中读取并打印整行。

private void button_print_results_Click(object sender, RoutedEventArgs e)
{
    int counter = 0;
    string actual_line;


    System.IO.StreamReader file_to_read =
    new System.IO.StreamReader("myText.txt");
    while ((actual_line = file_to_read.ReadLine()) != null)
    {

        textBox_results.Text = textBox_results.Text +"\n"+ actual_line;
        counter++;
    }

    file_to_read.Close();
}

有没有办法在不覆盖整个文件的情况下实现这一点?

不,我无法更改名称等的保存格式。 (我在这里使用它们是为了更好地理解,我需要阅读/检查的实际行是不同的并且是自动生成的)。

我不希望有完整的工作代码,如果您能告诉我需要查看哪些命令,那就太好了。自从我上次使用 c#/wpf 以来已经很长时间了,而且我从来没有使用过 Streamreader...

谢谢

【问题讨论】:

    标签: c# .net wpf streamreader


    【解决方案1】:

    我认为正则表达式是您想要实现的最佳工具。你可以这样写:

    Regex regex = new Regex("Logged in (?<userName>.+) on (?<loginTime>.+)");
    while ((actual_line = file_to_read.ReadLine()) != null)
    {
        Match match = regex.Match(actual_line);
        if (match.Success) {
            string loginInfo = string.Format("{0} - {1}", match.Groups["userName"], match.Groups["loginTime"]);
            textBox_results.Text = textBox_results.Text +"\n"+ loginInfo;
        }
    }
    

    【讨论】:

    • Regex 是这里最好的方法,尽管如果日期和时间格式始终相同,您可能希望使用更具体的模式。我想出了Logged in (?&lt;username&gt;{.+}) on (?&lt;date&gt;\d+\/\d+\/\d+) at (?&lt;time&gt;\d+:\d+ ?[ap]m)
    【解决方案2】:

    对此有几种可能的解决方案。对于您的情况,一种最直接的方法是使用SubstringReplace

    由于前面的string 始终是Logged in(注意最后一个空格),而您只想在短语后面得到string 的其余部分,只替换时间词的介词(“on”,“在 ") 和破折号 (" - ") 你可以利用这一点:

    string str = "Logged in {username} on 13/04/2016 at 10:55 am";
    string substr = str.Substring(("Logged in ").Length) //note the last space
                       .Replace(" on ", " - ")
                       .Replace(" at ", " - ");
    

    在您的实现中,它是这样的:

    while ((actual_line = file_to_read.ReadLine()) != null)
    {
        actual_line = actual_line.Substring(("Logged in ").Length) //note the last space
                       .Replace(" on ", " - ")
                       .Replace(" at ", " - ");
        textBox_results.Text = textBox_results.Text +"\n"+ actual_line;
        counter++;
    }
    

    (注意:上面的解决方案假定{username} 确实包含时间词的间隔介词 - {username} 几乎可能是这种情况)

    【讨论】:

      【解决方案3】:

      您可以拆分 actual_line 字符串,以便获得一个字符串数组。然后将要在TextBox中显示的字符串填入其中。

      string[] values = actual_line.Split(' ');
      textBox_results.Text = textBox_results.Text + "\n" + values[2] + " " + values[6] + " " + values[7];
      

      例如,TextBox 中的文本是“{username} 10:55 am

      【讨论】:

      • 如果用户名不包含空格也可以,除非这个解决方案是有效的
      【解决方案4】:

      正如@Dmitry-Rotay 在之前的评论中所建议的那样,您可以使用正则表达式来获得更好的性能,但是如果您的文件不是那么大,那么您的循环+字符串操作是一个可以接受的折衷方案。

      始终使用 Environment.NewLine 而不是“\n”,它更便携。

      while ((actual_line = file_to_read.ReadLine()) != null)
      {
          actual_line = actual_line
                     .Replace(("Logged in "), String.Empty)
                     .Replace(" on ", " - ")
                     .Replace(" at ", " - ");
          textBox_results.Text = textBox_results.Text 
                     + System.Environment.NewLine 
                     + actual_line;
          counter++;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-03
        • 2021-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多