2014-09-30 84 views
0

我试图找到处理我遇到的问题的最佳方法。我需要能够从字符串中提取注释,这些字符串被视为字符串末尾括号内的内容。评论可以是单个,多个,嵌套或这些的组合。正则表达式正确地处理嵌套模式

一些例子:

this is a string (with comment) 
this is another string (with comment)(and more comment) 
this is yet another string (with comment (and some nested comment) 

这是最简单的形式,很容易使用下面的正则表达式来分离(进入VBA)

regex.Pattern = "^([^(]*)(\(.*\))+$" 

我得到以下正确的输出,其中1组为值和组2将是评论

group1: this is a string/group2: (with comment) 
group1: this is another string/group2: (with comment)(and more comment) 
group1: this is yet another string/group2: (with comment (and some nested comment) 

问题是,在有些情况下我有数组,这些都会失败。数组可以用逗号或斜线定义。非常简单,但问题是这些令牌也可以用于其他目的。所以,如果一个逗号或斜线的字符串中发现它被认为是一个数组,除非:

- the token is within the comment 
- the slash is part of a fractional number 

一些例子:

this is string1 with a fractional 1/4 number (with comment) 
this is string1 (with a fractional 1/4 in comment) 
this is string1 (with comment1)/this is string2 (with comment2) 
this is string1 (with some data, seperated by a comma) , this is string2 (with comment3/comment4) 
this is string1 (with a fractional 1/4)/this is string2 (with comment2,comment3) 

补充的例子:第一,因为它包含数组令牌一个出现故障(在斜线),这不是小数的一部分。第二个选择太多,因为它只应该从第一个到第二个评论取最后的评论而不是整个字符串。

this is string1 without comment/this is string2 (with comment2) 
This is a string (with subcomment) where only the last should be selected (so this one) 

我将如何调整逻辑的最佳使得其可以在重复失败,除非逗号或斜线是例外的一部分?我结束了怪物代码,所以想看看是否有更容易的选项。所以,上述例外情况应该结束了如下:

ex1/group1 : this is string1 with a fractional 1/4 number group2: (with comment) 
ex2/group1 : this is string1 group2 : (with a fractional 1/4 in comment) 
ex3 to 5 should fail as they are considered arrays and need some additional logic 

希望这是一个有点清楚..

回答

1

我想你想这样的事情,

^((?:(?!\)\s*[,\/]).)*?)(\([^()]*\))$ 

DEMO

更新:

^(?=(?:(?!\)\s*[,\/]|\s\/\s).)*$)(.*?)((?:\([^()\n]*\))+)$ 

DEMO

+0

很近,太棒了!它现在不再与'更简单的'完全匹配了,因为[这是另一个字符串(有评论)(还有更多评论)],但我会从这里试试 – Wokoman 2014-09-30 12:46:11

+0

上述输入的输出是什么? – 2014-09-30 12:48:22

+0

第一组应该是[这是另一个字符串],第二组应该是注释[(带注释)(和更多评论)],所以任何重复的括号内的数据。 – Wokoman 2014-09-30 12:55:21