提取与模式匹配的所有子字符串

时间:2018-05-07 12:18:54

标签: c# regex string sqlite split

我有一个超市数据库的查询工作量,我需要分析。我有很多查询,它们作为字符串存储在列表中。它们具有以下形式:

  SELECT ... FROM ... WHERE Price = '29' AND Quantity = '2';

  SELECT ... FROM ... WHERE Price = '10'Discount = '5' AND Category = 'Fruit';

现在我希望将List作为输出,其中包含以下项目:

   Price = '29'
   Quantity = '2'
   Price = '10'
   Discount = '5'
   Category = 'fruit'

来自每个查询。我尝试使用正则表达式' ='作为模式,但我无法弄清楚如何在该角色之前和之后提取整个单词。

1 个答案:

答案 0 :(得分:-1)

你去吧

string input = "SELECT ... FROM ... WHERE Price = '10' AND Discount = '5' AND Category = 'Fruit';";
List<string> result = Regex.Matches(input, @"\w+ = '\w+'")
                           .Cast<Match>()
                           .Select(x => x.Value)
                           .ToList();