C#多行多列电子邮件阅读器提取器

时间:2019-10-03 13:42:08

标签: c# .net regex parsing

所以我用C#制作了一个控制台应用程序,可以读取电子邮件并从中提取数据。

在一些帮助下,我可以将其读入成对的阶段,但是当我触及电子邮件的底部时(可能比这两行还要多),它就无法分解。

这是我尝试过的:

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace Multiline_Email_Test
{
// <summary>
/// Console app to test the reading of the multiline email.
/// If successful readback is shown we could import to SQL Server.
/// </summary>
public class Program
{
    public static void Main()
    {
        string email = @"NOTIFICATION OF MOVEMENT STARTING IN AUGUST

Consignor Package ID                              Local Reference Number
-------------------                              ----------------------
GRLK123450012                                         123456

Place Of dispatch                                Guarantor type code
-----------------                                -------------------
GR00001234567                                          1

Consignee Package ID                              Guarantor details
-----------------                                -------------------
RR001239E0070

Place Of delivery                                Date of dispatch DD MM YYYY
-----------------                                ---------------------------
FR001379E0570                                    21 03 2019

                                                 Time of dispatch
                                                 ----------------
                                                 08:29

                                                Vehicle registration number
                                               ---------------------------
                                               XXBB12345678

Item number   Package Product CN CodeCode    Quantity       Brand
-----------   -------------------------     --------       -----
Line 1 of 2   B000           22040009       7603.200       Guinness DIC    440ml CAN 06X04 MDCES
Line 2 of 2   B000           22040009       14636.160      Guinness DIC    440ml CAN 06X04 MDCES

";



var dict = new Dictionary<string, string>();
        try
        {
            var lines = email.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            int starts = 0, end = 0, length = 0;
            while (!lines[starts + 1].StartsWith("-"))
                starts++;
            for (int i = starts + 1; i < lines.Length; i += 3)
            {
                var mc = Regex.Matches(lines[i], @"(?:^| )-");
                foreach (Match m in mc)
                {
                    int start = m.Value.StartsWith(" ") ? m.Index + 1 : m.Index;
                    end = start;
                    while (lines[i][end++] == '-' && end < lines[i].Length)
                        ;
                    length = Math.Min(end - start, lines[i - 1].Length - start);
                    string key = length > 0 ? lines[i - 1].Substring(start, length).Trim() : "";
                    end = start;
                    while (lines[i][end++] == '-' && end < lines[i].Length)
                        ;
                    length = Math.Min(end - start, lines[i + 1].Length - start);
                    string value = length > 0 ? lines[i + 1].Substring(start, length).Trim() : "";
                    dict.Add(key, value);
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }

        foreach (var x in dict)
            Console.WriteLine("{0} : {1}", x.Key, x.Value);
       }
   }
}

我已经在.net中创建了一个现场演示 https://dotnetfiddle.net/6nMO2c

1 个答案:

答案 0 :(得分:1)

关于文档的标头值,您的代码似乎可以正常工作,但只是出于娱乐目的,我找到了一个可以完成此工作的正则表达式。 然后我还要回答有关表数据的问题。

        int textArrayPosition = 0; // Just to separate the header part and the table part
        var headersDictionary = new Dictionary<string, string>();
        List<string> arrayHeaders;
        List<List<string>> arrayData = new List<List<string>>();
        var headersFinder = new Regex(@"^(.*?) {2,}(.*)\r\n\-*? {2,}\-*\r\n(.*?)( {2,}(.*)|$)", RegexOptions.Multiline);

        foreach (Match match in headersFinder.Matches(inputText))
        {
            if (match.Groups.Count < 4)
                continue;

            var firstHeaderName = match.Groups[1].Value;
            var secondHeaderName = match.Groups[2].Value;

            if (!string.IsNullOrWhiteSpace(firstHeaderName))
                headersDictionary.Add(firstHeaderName, match.Groups[3].Value);

            if (!string.IsNullOrWhiteSpace(secondHeaderName))
            {
                if (match.Groups.Count == 6)
                    headersDictionary.Add(secondHeaderName, match.Groups[5].Value);
                else
                    headersDictionary.Add(secondHeaderName, string.Empty);
            }

            textArrayPosition = match.Index + match.Length;
        }

        Console.WriteLine("*** Document headers :");
        foreach (var entry in headersDictionary)
            Console.WriteLine($"{entry.Key} = {entry.Value}");

然后,我们在您的文本表中找到行列表。

 var arrayLines = inputText.Substring(textArrayPosition).Split(new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);

因此,我们对待表:由于表的标题不允许分隔列,因此我本人基于这样的事实:在数据的第一行中至少找到两个连续的空格以能够猜测位置列。一个简单的正则表达式可以帮助我们做到这一点。

        if (arrayLines.Length > 2)
        {
            var arrayColsPositions = new List<int>();

            // Find cols positions
            arrayColsPositions.Add(0);
            var firstDataLine = arrayLines[2];
            var columnsPositionDetector = new Regex(@" {2,}", RegexOptions.Singleline);
            foreach (Match match in columnsPositionDetector.Matches(firstDataLine))
            {
                arrayColsPositions.Add(match.Index + match.Length);
            }

            // Find headers
            arrayHeaders = ReadLineValues(arrayLines[0], arrayColsPositions).ToList();
            // Find data lines
            for (int lineId = 2; lineId < arrayLines.Length; lineId++)
            {
                arrayData.Add(ReadLineValues(arrayLines[lineId], arrayColsPositions).ToList());
            }

            Console.WriteLine("\n*** Array headers :");
            Console.WriteLine(string.Join(", ", arrayHeaders));

            Console.WriteLine("\n*** Array lines data :");
            foreach (var record in arrayData)
            {
                Console.WriteLine(string.Join(", ", record));
            }
        }
        else
            Console.WriteLine("The array is empty.");

最后,这是我开发的一种实用工具,可以在不超过某些行的长度的情况下很好地搜索数据,以在正确的位置进行查找。

    private static IEnumerable<string> ReadLineValues(string sourceLine, List<int> colsPositions)
    {
        for (int colId = 0; colId < colsPositions.Count; colId++)
        {
            var start = colsPositions[colId];
            int length;
            if (colId < colsPositions.Count - 1)
                length = colsPositions[colId + 1] - start;
            else
                length = sourceLine.Length - start;

            if (start < sourceLine.Length)
            {
                if (start + length > sourceLine.Length)
                    length = sourceLine.Length - start;

                yield return sourceLine.Substring(start, length).Trim();
            }
        }
    }