【问题标题】:How to get strings before and after specified text, then print to txt in C#?如何在指定文本之前和之后获取字符串,然后在 C# 中打印到 txt?
【发布时间】:2018-05-16 12:33:31
【问题描述】:

我有一个非常大的txt 文件(称为input.txt)。大部分信息对我来说是不必要的,但是有很多 iD-s,它们在 id=ampstrings 之间。 我想将每个 id 写入一个新的 txt 文件 (output.txt) ,其中每个 iD 都在一个新行中。

我该如何管理?

https://pastebin.com/5tqAiPUi -- 示例 txt

期望的输出:

1839708603
1845432669
1850285729
100000000530931
100000011404225

【问题讨论】:

  • 通过解析输入文件。
  • 我该怎么做?
  • 请提供文件的示例(相关摘录)和期望的结果
  • @Brúnó Szubally 我的意思是,你应该先研究一下,然后再在这里提问。我们宁愿帮助解决问题,也不愿为您完成全部工作
  • 从您提供的小细节来看,听起来您正在处理 URL 及其查询参数。将它们视为 URI 将更容易提取所需的信息。如果实际上您正在解析日志文件,则LogParser 是首选工具,而不是您自己解析字符串。

标签: c# string text split


【解决方案1】:

你可以试试正则表达式

  using System.Text.RegularExpressions;
  using System.Linq;

  ...

  string text = ...;

  string[] ids = Regex
    .Matches(text, "(?:id=)(?<value>[0-9]+)(?:&amp)")
    .OfType<Match>()
    .Select(match => match.Groups["value"].Value)
    .ToArray();

如果您想从/向文件读取/写入数据:

 File.WriteAllLines(@"c:\Output.txt", Regex
    .Matches(File.ReadAllText(@"c:\Input.txt"), "(?:id=)(?<value>[0-9]+)(?:&amp)")
    .OfType<Match>()
    .Select(match => match.Groups["value"].Value)); 

【讨论】:

    【解决方案2】:

    我认为这段代码可以帮助你提取:

            const string startString = "id=";
            const string endString = "amp";
    
            string test = "ffvreergverfverid=38338ampvevbevvid=3amp";
            StringBuilder outfile = new StringBuilder();
            do
            {
                int startPos = test.IndexOf(startString);
                int endPos = test.IndexOf(endString);
                outfile.AppendLine(test.Substring(startPos, endPos - startPos));
                test = test.Remove(startPos, (endPos + endString.Length)- startPos);
            }while(test.Contains(startString));
    

    【讨论】:

      【解决方案3】:

      这看起来像您正在阅读网址

      我个人会查看StreamReader 课程,您需要阅读每个字符,直到找到一系列字符,例如:

          using System;
          using System.Collections.Generic;
          using System.IO;
          using System.Linq;
          using System.Text;
          namespace StrReader
          {
              class Program
              {
                  static void Main(string[] args)
                  {
                      bool hit = false;
                      string start = "?id=";
                      string end = "&amp;";
                      string buffer = string.Empty;
                      string endBuffer = string.Empty;
                      using(StreamReader sr = new StreamReader(@"C:\development\zaza.txt"))
                      {
                          while (sr.Peek() >= 0)
                          {
                              string value = ((char)sr.Read()).ToString();
                              if(!hit){
                                  if (start.IndexOf(value) > -1)
                                      buffer = string.Concat(buffer, value);
                                  else buffer = string.Empty;
                                  hit = string.Equals(buffer, start, StringComparison.CurrentCultureIgnoreCase);
                                  if (buffer.Length >= start.Length && hit)
                                      buffer = string.Empty;
                              }
                              else
                              {
                                  if (end.IndexOf(value) > -1)
                                      endBuffer = String.Concat(endBuffer, value);
                                  else
                                      endBuffer = string.Empty;
                                  buffer = string.Concat(buffer, value);
                                  if (endBuffer == end)
                                  {
                                      Console.WriteLine(buffer.Substring(0,buffer.Length - endBuffer.Length ));
                                      buffer = string.Empty;
                                      hit = false;
                                  }
                                  buffer = string.Concat(buffer, value);
                              }
                          }
                      }
                      Console.ReadLine();
                  }
              }
          }
      

      您要读取每个字符的原因是,如果您将整个文件读入内存,您会很伤心,这会大大降低您的机器速度。

      上面代码中的一些 cmets 将 c:\development\zaza.txt 更改为大文件,您还需要将开始标识符 ?id= 更改为您需要的内容。最后,需要根据您的要求更改结束标识符 (&)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-19
        • 1970-01-01
        • 2022-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多