【问题标题】:How can I use a Regex to get specific values from a text file delimitted by ,\n?如何使用正则表达式从由 ,\n 分隔的文本文件中获取特定值?
【发布时间】:2012-06-15 14:57:32
【问题描述】:

我有一个如下的 SQL 脚本,用于用一些数据填充 db 表。 然后我在 VS2010 中使用 C# 中的 StreamReader 读取此文件。我想知道的是,一旦我将此文件作为字符串读入,如何将每个单独的参数拆分为子字符串?

所以理想情况下,我想要将每个单独的 VALUE 参数读入它自己的单独子字符串,以便我可以处理它。

SQL 脚本:

...

INSERT INTO [dbo].[My_Table] ( \n My_ID, \n My_Title, \n My_Message \n ) VALUES ( \n 40, \n 'Hello, This is the message title', \n 'Hello, This is \n the message body' \n )

INSERT INTO [dbo].[My_Table] ( \n My_ID, \n My_Title, \n My_Message \n ) VALUES ( \n 41, \n 'Hello again, This is another message title', \n 'Hello again, This is \n another message body' \n )

我目前正在调试它并尝试几种不同的方法,一种使用 String.Split() ,另一种使用 Regex 方法。

这是我的 C# 代码:

// this is to find the VALUES parameters in the SQL file
private static readonly Regex matchValues = new Regex(@".*?VALUES.*?\((.*?)\)",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
|RegexOptions.Singleline);

// fileText is the string object containing the raw text read in from the SQL file
public static string FindMatches(string fileText)
{
    List<Match> matches = matchValues.Matches(fileText).Cast<Match>().ToList();

    foreach (Match match in matches)
    {
         string value = match.Groups[1].Value;
         string pattern = @"^,$";

         // do work

         string[] delimiters = new string[] { ",\n" };

         string[] splitGroup = value.Split(delimiters, StringSplitOptions.None);

         string[] split = Regex.Split(value, pattern);

     }
}

所以,如果我能简要解释一下这段代码,matchValues 正则表达式会为我找到插入参数的值,并且工作正常。 (注意我已经用 \n 字符更新了 SQL 文件,以显示文件的布局以及读取时它是如何存储在字符串变量中的)。请注意,在 My_Message 值中可以有 ',' 和 '\n' 情况。但是,每个参数的结尾可以由 ',\n' 唯一标识,但我无法让它在正则表达式中工作,并且 String.Split() 只能使用 1 个字符。

列表包含每个发现的匹配项的每个案例,因为我在 SQL 脚本中有 50 多个条目,因此我需要将每个插入语句中的每个单独的 ID、标题和消息拆分为 3 个单独的变量,这些变量嵌套在循环中.

当前 splitGroup[] 字符串对象返回了太多子字符串,因为我们在参数值中有新行,而使用正则表达式的 split[] 字符串对象只是将其全部作为一个字符串返回。

我希望此更新信息对您有所帮助。
提前致谢!

【问题讨论】:

  • 为什么是正则表达式而不是string.Split()
  • 嗨@Jeremy,我已经更新了我的问题,使其更加具体,并解释了为什么 string.Split() 目前不适合我。谢谢

标签: c# regex string parsing substring


【解决方案1】:

您可以设置 RegexOptions 以匹配数据多行,这意味着正则表达式将匹配美元符号 $ 与行尾而不是字符串结尾。代码如下:

string strRegex = @"^Regex Test";
RegexOptions myRegexOptions = RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"Regex Test for stackoverflow.";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
    // Add your code here
  }
}

【讨论】:

    【解决方案2】:

    你也可以使用String.Split:

    var inserts = File.ReadLines(path)
             .Where(l => l.IndexOf("VALUES (") > -1)
             .Select(l => new
             {
                 SQL = l,
                 Params = l.Substring(l.IndexOf("VALUES (") + 8)
                           .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
             });
    foreach (var insert in inserts)
    {
        String sql = insert.SQL;
        String[] parameter = insert.Params;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多