在字符串 C# 的 DataTable 中查找子字符串的所有实例

Finding all instances of substring within DataTable of strings C#

如标题所示,我目前有一个数据表,其中只有一列,多行包含字符串。我希望从每一行中提取 'Order Number' 的所有实例,每行可能有多个订单号。

下面是具有多个订单号的数据表中单行的示例:

"\r\nYour Dispatch Advice Confirmation was Successful \r\nThe details of the Dispatch Advice Confirmation are shown below. \r\n\r\nSite Name: Germany \r\nCountry: Germany \r\nInvestigator Name: Inv Name \r\nOrder Number: 111 \r\nActual Date of Dispatch: 26/Apr/2021 \r\nActual Time of Dispatch: 14:01 \r\nLatest Acceptable Date of Receipt: 29/Apr/2021 \r\nLatest Acceptable Time of Receipt: 14:01 \r\nCourier: \r\nAirway Bill Number: \r\nReference: \r\nNon-Uniquely Labelled Medication:- \r\nTotal Quantity of Kits: 20 \r\n\r\nSite Name: Germany \r\nCountry: Germany \r\nInvestigator Name: Inv Name \r\nOrder Number: 112 \r\nActual Date of Dispatch: 26/Apr/2021 \r\nActual Time of Dispatch: 07:00 \r\nLatest Acceptable Date of Receipt: 29/Apr/2021 \r\nLatest Acceptable Time of Receipt: 14:01 \r\nCourier: \r\nAirway Bill Number: \r\nReference: \r\nNon-Uniquely Labelled Medication:- \r\nTotal Quantity of Kits: 10

基本上我需要遍历数据表中的每一行并提取每个订单号(仅数字),然后将它们转储到一个 int 列表或类似的东西中。 老实说,我不知道如何进行,因为这是我第一次使用数据表:

public void generateResponseList(Schedule theSchedule)
    {
        string sql = "SELECT Body FROM Responses WHERE responseDefID = 4 AND ResponseType = 1";

        sql = Utils.GetSQL(theSchedule.ParentStudy, sql);
        System.Data.DataTable table = theSchedule.ParentStudy.TheDatabase.GetDataTable(sql);

        string test2;
        foreach(System.Data.DataRow row in table.Rows)
        {
            test2 = row["Body"].ToString();
            //var value = row["Body"].ToString().Substring(0, row["Body"].ToString().LastIndexOf('=') + 1);
        }
    }

目前 test2 将等于每一行,上面列出的文本的 sperg。

非常感谢任何帮助!

编辑: 我注意到,由于我转换为字符串,上面的输出可能格式不正确。正确查看数据集可视化工具格式的行:

Your Dispatch Advice Confirmation was Successful The details of the Dispatch Advice Confirmation are shown below.

Depot: Grenzach (EU) Depot Site Number: 49001 Site Name: Germany Country: Germany Investigator Name: Inv Name Order Number: 111 Actual Date of Dispatch: 26/Apr/2021 Actual Time of Dispatch: 14:01 Latest Acceptable Date of Receipt: 29/Apr/2021 Latest Acceptable Time of Receipt: 14:01 Courier: Airway Bill Number: Reference: Non-Uniquely Labelled Medication:- Total Quantity of Kits: 20

Depot: San Francisco (USA) Depot Site Number: 49001 Site Name: Germany Country: Germany Investigator Name: Inv Name Order Number: 112 Actual Date of Dispatch: 26/Apr/2021 Actual Time of Dispatch: 07:00 Latest Acceptable Date of Receipt: 29/Apr/2021 Latest Acceptable Time of Receipt: 14:01 Courier: Airway Bill Number: Reference: Non-Uniquely Labelled Medication:- Total Quantity of Kits: 10

我无法想象这会改变子字符串调用太多但值得牢记!

已解决

对于将来偶然发现此问题的任何人,必须稍微编辑正则表达式以说明“订单号:”和数字之间的白色 space。也可以通过删除我想象的所有白色 spaces 来实现。 也改为输出到字符串列表:

public void generateResponseList(Schedule theSchedule)
    {
        //generates respList of orders that have recieved responses
        string sql = "SELECT Body FROM Responses WHERE responseDefID = 4 AND ResponseType = 1";

        sql = Utils.GetSQL(theSchedule.ParentStudy, sql);
        System.Data.DataTable table = theSchedule.ParentStudy.TheDatabase.GetDataTable(sql);

        var respList = new List<string>();

        string pattern = @"Order Number: (?'OrderNum'\s\d+)";
        foreach (DataRow row in table.AsEnumerable())
        {
            string data = row.Field<string>(0);
            MatchCollection matches = Regex.Matches(data, pattern);
            respList.AddRange(matches.Cast<Match>().Select(x => x.Groups["OrderNum"].Value).ToArray());
        }
    }

使用正则表达式:

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

namespace ConsoleApplication189
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Body",typeof(string));

            dt.Rows.Add(new object[] { " Name Order Number: 112 \n  Name Order Number: 113 \n  Name Order Number: 114" });
            dt.Rows.Add(new object[] { " Name Order Number: 212 \n  Name Order Number: 213 \n  Name Order Number: 214" });
            dt.Rows.Add(new object[] { " Name Order Number: 312 \n  Name Order Number: 313 \n  Name Order Number: 314" });
            dt.Rows.Add(new object[] { " Name Order Number: 412 \n  Name Order Number: 413 \n  Name Order Number: 414" });
            dt.Rows.Add(new object[] { " Name Order Number: 512 \n  Name Order Number: 513 \n  Name Order Number: 514" });


            string pattern = @"Order Number: (?'OrderNum'\d+)";
            foreach (DataRow row in dt.AsEnumerable())
            {
                string data = row.Field<string>(0);
                MatchCollection matches = Regex.Matches(data, pattern);
                string[] ordNums = matches.Cast<Match>().Select(x => x.Groups["OrderNum"].Value).ToArray();
                Console.WriteLine(string.Join(",", ordNums));
            }

            Console.ReadLine();
 
        }
    }
  
}