开发者

c# regular expression match even-parenthesis in expression

I have the following expressions:

KNOWN_TOKEN=((value operator value) operator value) 
            operator OTHER_KNOWN_TOKEN=value 
            operator KNOWN_TOKEN2=(value operator (value operator value));

I am trying to find a c# regular expression to return me the entire expression with equally matched closed parenthesis as they were opened. This means, for KNOWN_TOKEN I need only ((value operator value) operator value), for OTHER_KNOWN_TOKEN I need only value and for KNOWN_TOKEN2 I need (value operator (value operator value)).

I tried various flavours of (\([^(]+\)) but they only match the 'lowest' set of 'paranthesis' and in addition, they match an extra one (i.e. for KNOWN_TOKEN2 they match an extra ending parenthesis).

Also, I have found some hints on the web on doing it into several repe开发者_运维知识库ating steps and alter the original text, but I'd rather do it into one single regexp. Any hints?

Thank you!


Regex reg = new Regex(@"(?<token>[^=]*)=(?<value>\(*value.*)");
foreach (Match item in reg.Matches(""))
{
      var token= item.Groups["token"].Value.Trim();
      var val= item.Groups["value"].Value.Trim(';');
}

EDITED

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜