开发者

Parse Excel formula "=a(b,c,d)" with Regex in vb.net

I am trying to parse the parameters of Excel formulas like "=a(b)", "=a(b,c)", "=a(b,c,d)". I'd like extract the function name "a" and the parameters "b", "c" and "d".

There are loads of examples on SO to parse HTML and so forth, but none specifically for parentheses.

So far, I've got this "=(.+)\(([^,)]*)(,[^,)]*)*\)" but when I parse "=a(b,c,d)" it puts "a" in match(1), "b" in match(2) and ",d" in match(3). So "c" is lost and the comma before the "d" is a pain.

How can I par开发者_如何学Gose a string like this with an arbitrary number of parameters, ideally dropping the commas?

The ability to parse "=a(b(c),d(e(f)))" would be great...


Edit: I know that a parser is the correct solution and I have used Devin Cook's excellent Gold Parser with great results before.

However, the particular case I'm facing is to extract the arguments from a known Excel formula. Specifically, if the formula contains the string "=Travel(", I know that it will have 4 arguments and if they don't parse it's not a problem. This is simply a "nice to have" function which can fail occasionally without it being an issue.

Could someone possibly help me with a regex for "=a(b,c,d)", "=a(b,c,d,e)", etc., with the constraint that there will be no nested parentheses or commas?

Thanks!


Does this help? Regex to parse functions with arbitrary depth

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜