C# 如何获取指定文本前后的字符串,然后在C中打印为txt?

C# 如何获取指定文本前后的字符串,然后在C中打印为txt?,c#,string,text,split,C#,String,Text,Split,我有一个很大的txt文件,叫做input.txt。大多数信息对我来说都是不必要的,但是有很多iD-s,它们位于iD=和AMPS字符串之间。 我想将每个id写入一个新的txt文件output.txt,其中每个id都在一个新行中 我该怎么办 -示例txt 期望输出: 1839708603 1845432669 1850285729 100000000530931 100000011404225 您可以尝试使用正则表达式: 如果要从文件中读取/写入数据,请执行以下操作: File.WriteAll

我有一个很大的txt文件,叫做input.txt。大多数信息对我来说都是不必要的,但是有很多iD-s,它们位于iD=和AMPS字符串之间。 我想将每个id写入一个新的txt文件output.txt,其中每个id都在一个新行中

我该怎么办

-示例txt

期望输出:

1839708603
1845432669
1850285729
100000000530931
100000011404225
您可以尝试使用正则表达式:

如果要从文件中读取/写入数据,请执行以下操作:

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

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

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

看起来您正在阅读URL的

我个人会在课堂上检查,你需要阅读每个字符,直到你找到一系列字符,比如:

    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();
            }
        }
    }
要读取每个字符的原因是,如果您将整个文件读入内存,您会感到悲伤,这将严重降低您的机器速度


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

通过解析输入文件。我如何才能做到这一点?请提供文件示例、相关摘录和所需的outcome@Brúnószubaly我的意思是,在这里提问之前,你应该进行研究并付出一些努力。我们更愿意帮助解决问题,而不是为您完成全部工作。从您提供的小细节来看,您似乎正在处理URL及其查询参数。将这些视为URI将使提取所需信息更加容易。如果事实上您正在解析日志文件,那么它将是您选择的工具,而不是您自己解析字符串。
    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();
            }
        }
    }