删除正则表达式中不必要的括号
假设我(在 JavaScript 正则表达式中)
((((A)B)C)D)
当然是这样的:
ABCD
是否有一种算法可以消除这样的字符串中不必要的括号?
Suppose I have (in a javascript regular expression)
((((A)B)C)D)
Of course that really reads
ABCD
Is there an algorithm to eliminate unnecessary parentheses in a string like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此函数将删除所有后面没有量词的组,并且不是环视。它假定 ECMAScript 风格的正则表达式,并且捕获组 (
(
...)
) 并不重要。示例:
它不会尝试确定括号是否仅包含单个标记 (
(A)?
)。这将需要更长的标记化模式。This function will remove all groups that is not followed by a quantifier, and is not a look-around. It assumes ECMAScript flavor regex, and that capture-groups (
(
...)
) are unimportant.Examples:
It does not try to determine if the parenthesis contains only a single token (
(A)?
). That would require a longer tokenizing pattern.1)使用理解括号的解析器
2)使用可以匹配括号的Perl递归正则表达式(恕我直言,在这种情况下不鼓励)我不认为Boost正则表达式支持所需的递归类型。
3)也许需要它们?别管他们了。
1) Use a parser that understands parenthesis
2) Use a Perl recursive regex that can match parenthesis (discouraged in this case IMHO) I don't think Boost regex's support the type of recursion needed.
3) Perhaps they are needed? Leave them alone.