您可以使用此:
(?>w+.)?w+((?>((?<DEPTH>)|)(?<-DEPTH>)|[^()]+)*)(?(DEPTH)(?!))|w+
通过您的示例,您可以获得:
0 => username1 => TB_PEOPLE.fields(FirstName,LastName,TB_PHONE.fields(num_phone1, num_phone2))2 => password
说明:
(?>w+.)? w+ ( # the opening parenthesis (with the function name)(?> # open an atomic group ( (?<DEPTH>) # when an opening parenthesis is encountered, # then increment the stack named DEPTH | # OR ) (?<-DEPTH>) # when a closing parenthesis is encountered, # then decrement the stack named DEPTH | # OR [^()]+# content that is not parenthesis)* # close the atomic group, repeat zero or more times) # the closing parenthesis(?(DEPTH)(?!)) # conditional: if the stack named DEPTH is not empty # then fail (ie: parenthesis are not balanced)
您可以使用以下代码尝试:
string input = "username,TB_PEOPLE.fields(FirstName,LastName,TB_PHONE.fields(num_phone1, num_phone2)),password";string pattern = @"(?>w+.)?w+((?>((?<DEPTH>)|)(?<-DEPTH>)|[^()]+)*)(?(DEPTH)(?!))|w+";MatchCollection matches = Regex.Matches(input, pattern);foreach (Match match in matches){ Console.WriteLine(match.Groups[0].Value);}


