正则表达式嵌套的括号 [英] Regex nested parentheses

查看:405
本文介绍了正则表达式嵌套的括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串:

  A,B,C,D.E(F,G,H,I(J,K)),L,M,N
 

就知道告诉我怎样可以建立一个正则表达式返回我的括号内像这样只有第一层次:

  [0] = A,B,C,
[1] = D.E(F,G,H,i.j(K,L))
[2] = M,N
 

的目标将是保持具有嵌套操纵未来括号中的相同索引的节

感谢你。

修改

试图改善示例...

想象一下,我有这个字符串

 用户名,TB_PEOPLE.fields(名字,姓氏,TB_PHONE.fields(num_phone1,num_phone2)),密码
 

我的目标是把一个字符串转换为动态查询。 然后,不以TB_领域,我知道他们是主表中的字段,否则我知道括号内informandos领域,都涉及到另一个表。 但我有困难检索所有领域的第一级,因为我可以从相关表分开,我可以去递归地回收剩余的字段。

在年底,将有这样的:

  [0] =用户名,密码
[1] = TB_PEOPLE.fields(名字,姓氏,TB_PHONE.fields(num_phone1,num_phone2))
 

我希望我已经解释过好一点,不好意思。

解决方案

您可以使用此:

<$p$p><$c$c>(?>\w+\.)?\w+\((?>\((?<DEPTH>)|\)(?<-DEPTH>)|[^()]+)*\)(?(DEPTH)(?!))|\w+

使用您的例子中,你获得:

  0 =&GT;用户名
1 =&GT; TB_PEOPLE.fields(名字,姓氏,TB_PHONE.fields(num_phone1,num_phone2))
2 =&GT;密码
 

说明:

 (大于?\ W + \)? \ w + \(#左括号(与函数名)
(大于?#打开的原子团
    \((&LT;深度GT;)左括号时遇到#,
                     #然后递增堆栈命名的深度
  | # 要么
    \)(小于?-depth&GT;)一个右括号时遇到#,
                     #然后再递减堆栈命名的深度
  | # 要么
    [^()] +#的内容,是不括号
)*#关闭原子组,重复零次或多次
\)#右括号
(?(深度)(?!))#条件:如果堆栈命名的深度不空
                     #然后失败(如:括号不均衡)
 

您可以用这个code试试吧:

 字符串输入=用户名,TB_PEOPLE.fields(名字,姓氏,TB_PHONE.fields(num_phone1,num_phone2)),密码;
字符串模式= @(大于\ W + \?)\ w + \((&GT; \((小于深度GT;)| \)(小于????-depth&GT;)| [^()] + )* \)((深)(?!))| \ w +?;
MatchCollection匹配= Regex.Matches(输入,图案);
的foreach(在比赛中赛赛)
{
    Console.WriteLine(match.Groups [0]。价值);
}
 

I have the following string:

a,b,c,d.e(f,g,h,i(j,k)),l,m,n

Would know tell me how I could build a regex that returns me only the "first level" of parentheses something like this:

[0] = a,b,c,
[1] = d.e(f,g,h,i.j(k,l))
[2] = m,n

The goal would be to keep the section that has the same index in parentheses nested to manipulate future.

Thank you.

EDIT

Trying to improve the example...

Imagine I have this string

username,TB_PEOPLE.fields(FirstName,LastName,TB_PHONE.fields(num_phone1, num_phone2)),password

My goal is to turn a string into a dynamic query. Then the fields that do not begin with "TB_" I know they are fields of the main table, otherwise I know informandos fields within parentheses, are related to another table. But I am having difficulty retrieving all fields "first level" since I can separate them from related tables, I could go recursively recovering the remaining fields.

In the end, would have something like:

[0] = username,password
[1] = TB_PEOPLE.fields(FirstName,LastName,TB_PHONE.fields(num_phone1, num_phone2))

I hope I have explained a little better, sorry.

解决方案

You can use this:

(?>\w+\.)?\w+\((?>\((?<DEPTH>)|\)(?<-DEPTH>)|[^()]+)*\)(?(DEPTH)(?!))|\w+

With your example you obtain:

0 => username
1 => TB_PEOPLE.fields(FirstName,LastName,TB_PHONE.fields(num_phone1, num_phone2))
2 => password

Explanation:

(?>\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)

You can try it with this code:

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);
}

这篇关于正则表达式嵌套的括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆