用于括号匹配的RegEx表达式

时间:2016-05-12 00:10:13

标签: c# .net regex

我需要一些C#正则表达式的帮助,它可以采用如下字符串:

string input = "Test1='1' OR (Test2 = '2' OR (Test3 = '3')) OR (Test4 = '4')";

并返回匹配项:

1 => Test1='1' OR
2 => (Test2 = '2' OR (Test3 = '3'))
3 => OR
4 => (Test4 = '4')

1 个答案:

答案 0 :(得分:0)

查看Grouping Constructs in Regular Expressions

为了帮助您入门..这是您提供的输入的示例。

string pattern = @"(Test1='1' OR) \(Test2 = '2'";
Match match = Regex.Match(input, pattern);
    if (match.Success)
    {
       foreach(Group group in match.Groups)
       {
           Console.WriteLine(group);
       }
    }
Console.ReadKey();