从文本文件中提取字符串

String Extraction from Text File

我有一个非常奇怪的文本文件,其中包含一些我需要提取的数据。我真的不知道最好的方法,我需要有关如何做的最佳想法的指南。文本如下所示:

Picture of Data to Extract

文件基本上包含该数据。我需要为每个 "column" 数据提取每个参数 AA - FF。每个数字之间有一些空格..

  1. 是否可以找到单词 AA,移动到“--”字符之后并复制该数字直到有空格....并在该行中重复? 或者,
  2. 尝试将该数据放入由空格分隔的 table 中是否更容易?

不知道如何开始,因此寻求帮助。 问候, DH

我会阅读每一行并用空格分隔它们,扔掉空的。 这将为您留下一个字段列表。在此示例中,我以 List(Of List(Of string)) 的形式创建了一个 字段列表列表 。这会为每一行创建一个字段列表,我认为这使得迭代和存储它们变得容易。

Vb.net样本

Module Module1

    Sub Main()
        Dim fieldsOfEachLine As New List(Of List(Of String))

        Dim reader As IO.StreamReader = New IO.StreamReader("F:\SlyWorkspace\Whosebug\SpaceSeparatedText_47337929\sampledata.txt")
        Dim currentLine As String

        Do
            currentLine = reader.ReadLine()
            If currentLine IsNot Nothing Then
                fieldsOfEachLine.Add(currentLine.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries).ToList())
            End If
        Loop Until currentLine Is Nothing

        reader.Close()

        'once you have your fields neatly placed in a list, it's up
        'to you to decide what you want to do with them
        'for this sample, I will just spit them back out the screen using a \ to delimit the information I kept
        For Each item As List(Of String) In fieldsOfEachLine
            Console.WriteLine(String.Join("\", item))
        Next

        Console.ReadLine()

    End Sub

End Module

这是一个快速的 C# 示例。

using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharp_SpaceSeparatedText_47337929
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<string>> fieldsForEachLine = new List<List<string>>();
            string currentLine = string.Empty;
            using (System.IO.StreamReader sr = new System.IO.StreamReader(@"F:\SlyWorkspace\Whosebug\SpaceSeparatedText_47337929\sampledata.txt"))
            {
                while ((currentLine = sr.ReadLine()) != null)
                {
                    fieldsForEachLine.Add(currentLine.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToList());
                }
            }

            foreach (List<string> item in fieldsForEachLine)
            {
                Console.WriteLine(string.Join("/", item));
            }

            Console.ReadLine();
        }
    }
}

我的数据文件包含这个

                           1                      2                     3          
                bu                     AU                    BG
AA               --      929.2244             1074.4697                 1114.7150
BB               --        1.458             1074.4697              1114.7150
CC           --    456.4584    448.4     584.5
DD  -- 1234.458      789.456                             4898.45

我的输出是这样的

1
bu\AU\BG
AA\--9.224474.469714.7150
BB\--.45874.469714.7150
CC\--6.45848.44.5
DD\--34.4589.45698.45