将多行电子邮件解析为 var

我正在尝试解析一封多行电子邮件,这样我就可以获取电子邮件正文标题下的换行符上的数据。它看起来像这样:


EMAIL STARTING IN APRIL


Marketing ID                                     Local Number

-------------------                              ----------------------

GR332230                                         0000232323


Dispatch Code                                    Logic code

-----------------                                -------------------

GX3472                                           1


Destination ID                                   Destination details

-----------------                                -------------------

3411144

当我使用字符串阅读器 readline 时,我似乎在每个消息框上都得到了所有内容,尽管我想要的只是每个消息框下的数据------,如图所示


这是我的代码:


foreach (MailItem mail in publicFolder.Items)

{

    if (mail != null)                  

    {


        if (mail is MailItem)

        {


            MessageBox.Show(mail.Body, "MailItem body");

            // Creates new StringReader instance from System.IO

            using (StringReader reader = new StringReader(mail.Body))

            {

                string line;

                while ((line = reader.ReadLine()) !=null) 

                //Loop over the lines in the string.

                if (mail.Body.Contains("Marketing ID"))

                {

                    // var localno = mail.Body.Substring(247,15);//not correct approach


                    // MessageBox.Show(localrefno);

                    //MessageBox.Show("found");

                    //var conexid = mail.Body.Replace(Environment.NewLine);


                    var regex = new Regex("<br/>", RegexOptions.Singleline);



                    MessageBox.Show(line.ToString());

                }

            }

        }

        else

        {

            MessageBox.Show("Nothing found for MailItem");

        }

    }

}    

你可以看到我对它进行了多次尝试,甚至使用子字符串位置和正则表达式。请帮我从---.


收到一只叮咚
浏览 95回答 4
4回答

繁星点点滴滴

&nbsp;var dict = new Dictionary<string, string>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var lines = email.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int starts = 0, end = 0, length = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (!lines[starts + 1].StartsWith("-")) starts++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = starts + 1; i < lines.Length; i += 3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var mc = Regex.Matches(lines[i], @"(?:^| )-");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (Match m in mc)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int start = m.Value.StartsWith(" ") ? m.Index + 1 : m.Index;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end = start;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (lines[i][end++] == '-' && end < lines[i].Length - 1) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; length = Math.Min(end - start, lines[i - 1].Length - start);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string key = length > 0 ? lines[i - 1].Substring(start, length).Trim() : "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end = start;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (lines[i][end++] == '-' && end < lines[i].Length) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; length = Math.Min(end - start, lines[i + 1].Length - start);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string value = length > 0 ? lines[i + 1].Substring(start, length).Trim() : "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dict.Add(key, value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception("Email is not in correct format");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }现场演示使用正则表达式:&nbsp; &nbsp; &nbsp;var dict = new Dictionary<string, string>();&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var lines = email.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int starts = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (!lines[starts + 1].StartsWith("-")) starts++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = starts + 1; i < lines.Length; i += 3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var keys = Regex.Matches(lines[i - 1], @"(?:^| )(\w+\s?)+");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var values = Regex.Matches(lines[i + 1], @"(?:^| )(\w+\s?)+");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (keys.Count == values.Count)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < keys.Count; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dict.Add(keys[j].Value.Trim(), values[j].Value.Trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else // remove bug if value of first key in a line has no value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (lines[i + 1].StartsWith(" "))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dict.Add(keys[0].Value.Trim(), "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dict.Add(keys[1].Value.Trim(), values[0].Value.Trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dict.Add(keys[0].Value, values[0].Value.Trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dict.Add(keys[1].Value.Trim(), "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception("Email is not in correct format");&nbsp; &nbsp; &nbsp; &nbsp; }现场演示

泛舟湖上清波郎朗

这是一个假设您不需要标题的方法,信息按顺序和强制性出现。这不适用于包含空格或可选字段的数据。foreach (MailItem mail in publicFolder.Items){&nbsp; MessageBox.Show(mail.Body, "MailItem body");&nbsp; // Split by line, remove dash lines.&nbsp; var data = Regex.Split(mail.Body, @"\r?\n|\r")&nbsp; &nbsp; .Where(l => !l.StartsWith('-'))&nbsp; &nbsp; .ToList();&nbsp; // Remove headers&nbsp; for(var i = data.Count -2; lines >= 0; i -2)&nbsp; {&nbsp; &nbsp; data.RemoveAt(i);&nbsp; }&nbsp; // now data contains only the info you want in the order it was presented.&nbsp; // Asuming info doesn't have spaces.&nbsp; var result = data.SelectMany(d => d.Split(' '));&nbsp; // WARNING: Missing info will not be present.&nbsp; // {"GR332230", "0000232323", "GX3472", "1", "3411144"}}

阿晨1998

这是我的尝试。我不知道电子邮件格式是否可以更改(行、列等)。除了检查双空格(我的解决方案)之外,我想不出一种分隔列的简单方法。class Program{&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var emailBody = GetEmail();&nbsp; &nbsp; &nbsp; &nbsp; using (var reader = new StringReader(emailBody))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var lines = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const int startingRow = 2; // Starting line to read from (start at Marketing ID line)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const int sectionItems = 4; // Header row (ex. Marketing ID & Local Number Line) + Dash Row + Value Row + New Line&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Add all lines to a list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string line = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = reader.ReadLine()) != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lines.Add(line.Trim()); // Add each line to the list and remove any leading or trailing spaces&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = startingRow; i < lines.Count; i += sectionItems)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var currentLine = lines[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var indexToBeginSeparatingColumns = currentLine.IndexOf("&nbsp; "); // The first time we see double spaces, we will use as the column delimiter, not the best solution but should work&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var header1 = currentLine.Substring(0, indexToBeginSeparatingColumns);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var header2 = currentLine.Substring(indexToBeginSeparatingColumns, currentLine.Length - indexToBeginSeparatingColumns).Trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentLine = lines[i+2]; //Skip dash line&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indexToBeginSeparatingColumns = currentLine.IndexOf("&nbsp; ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string value1 = "", value2 = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (indexToBeginSeparatingColumns == -1) // Use case of there being no value in the 2nd column, could be better&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value1 = currentLine.Trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value1 = currentLine.Substring(0, indexToBeginSeparatingColumns);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value2 = currentLine.Substring(indexToBeginSeparatingColumns, currentLine.Length - indexToBeginSeparatingColumns).Trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(string.Format("{0},{1},{2},{3}", header1, value1, header2, value2));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static string GetEmail()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return @"EMAIL STARTING IN APRIL&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Marketing ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Local Number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ----------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GR332230&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0000232323&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dispatch Code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Logic code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -----------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GX3472&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Destination ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Destination details&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -----------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3411144";&nbsp; &nbsp; }}输出看起来像这样:营销 ID,GR332230,本地编号,0000232323 调度代码,GX3472,逻辑代码,1 目的地 ID,3411144,目的地详细信息,

慕仙森

用 Regex 做这件事不是一个好主意,因为它很容易忘记边缘情况,不容易理解,也不容易调试。很容易陷入正则表达式挂起 CPU 并超时的情况。(我还不能对其他答案发表任何评论。所以,在选择最终解决方案之前,请至少检查我的其他两个案例。)在您的情况下,以下 Regex 解决方案适用于您提供的示例。但是,还有一些额外的限制:您需要确保非开始或非结束列中没有空值。或者,如果有两列以上且中间的任何一列为空,都会导致该行的名称和值不匹配。不幸的是,我不能给你一个非正则表达式的解决方案,因为我不知道规范,例如:会有空格吗?会有 TAB 吗?每个字段的字符数是固定的还是灵活的?如果它是灵活的并且可以有空值,那么用什么样的规则来检测哪些列是空的?我假设它们很可能是由列名的长度定义的,并且只有空格作为分隔符。如果是这种情况,有两种方法可以解决它,两次通过 Regex 或编写自己的解析器。如果所有字段的长度都是固定的,那就更简单了:只需要使用子字符串来切割行,然后修剪它们。using System;using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions;public class Program{&nbsp; &nbsp; public class Record{&nbsp; &nbsp; &nbsp; &nbsp; public string Name {get;set;}&nbsp; &nbsp; &nbsp; &nbsp; public string Value {get;set;}&nbsp; &nbsp; }&nbsp; &nbsp; public static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var regex = new Regex(@"(?<name>((?!-)[\w]+[ ]?)*)(?>(?>[ \t]+)?(?<name>((?!-)[\w]+[ ]?)+)?)+(?:\r\n|\r|\n)(?>(?<splitters>(-+))(?>[ \t]+)?)+(?:\r\n|\r|\n)(?<value>((?!-)[\w]+[ ]?)*)(?>(?>[ \t]+)?(?<value>((?!-)[\w]+[ ]?)+)?)+", RegexOptions.Compiled);&nbsp; &nbsp; &nbsp; &nbsp; var testingValue =@"EMAIL STARTING IN APRILMarketing ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Local Number-------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ----------------------GR332230&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0000232323Dispatch Code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Logic code-----------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------------------GX3472&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1Destination ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Destination details-----------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------------------3411144";&nbsp; &nbsp; &nbsp; &nbsp; var matches = regex.Matches(testingValue);&nbsp; &nbsp; &nbsp; &nbsp; var rows = (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from match in matches.OfType<Match>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let row = (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from grp in match.Groups.OfType<Group>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select new {grp.Name, Captures = grp.Captures.OfType<Capture>().ToList()}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ).ToDictionary(item=>item.Name, item=>item.Captures.OfType<Capture>().ToList())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let names = row.ContainsKey("name")? row["name"] : null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let splitters = row.ContainsKey("splitters")? row["splitters"] : null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let values = row.ContainsKey("value")? row["value"] : null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where names != null && splitters != null &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; names.Count == splitters.Count &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (values==null || values.Count <= splitters.Count)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select new {Names = names, Values = values}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; var records = new List<Record>();&nbsp; &nbsp; &nbsp; &nbsp; foreach(var row in rows)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i< row.Names.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; records.Add(new Record{Name=row.Names[i].Value, Value=i < row.Values.Count ? row.Values[i].Value : ""});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; foreach(var record in records)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(record.Name + " = " + record.Value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出:Marketing ID&nbsp; = GR332230&nbsp;Local Number = 0000232323Dispatch Code&nbsp; = GX3472&nbsp;Logic code = 1Destination ID&nbsp; = 3411144Destination details =请注意,这也适用于此类消息:EMAIL STARTING IN APRILMarketing ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Local Number-------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ----------------------GR332230&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0000232323Dispatch Code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Logic code-----------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------------------GX3472&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1Destination ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Destination details-----------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3411144输出:Marketing ID&nbsp; = GR332230&nbsp;Local Number = 0000232323Dispatch Code&nbsp; = GX3472&nbsp;Logic code = 1Destination ID&nbsp; =&nbsp;Destination details = 3411144或这个:EMAIL STARTING IN APRILMarketing ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Local Number-------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ----------------------Dispatch Code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Logic code-----------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------------------GX3472&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1Destination ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Destination details-----------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------------------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3411144&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;输出:Marketing ID&nbsp; =&nbsp;Local Number =&nbsp;Dispatch Code&nbsp; = GX3472&nbsp;Logic code = 1Destination ID&nbsp; =&nbsp;Destination details = 3411144
打开App,查看更多内容
随时随地看视频慕课网APP