使用 REGEX 将 X(...) 替换为 X{...} [英] Use REGEX to replace X(...) with X{...}

查看:68
本文介绍了使用 REGEX 将 X(...) 替换为 X{...}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题来自将代码更新为使用初始化程序的 C++11.

The problem comes from updating code to C++11, which uses initialisers.

所以:

a = X(4);
b = X();
c = X( (1+2)*(3+4) );
void P : X(5) { foo(); }

成为

a = X{4};
b = X{};
c = X{ (1+2)*(3+4) };
void P : X{5} { foo(); }

我的 IDE (XCode) 支持 RegEx 搜索和替换.

My IDE (XCode) supports RegEx search and replace.

我试过了:

X\((.*)\)  ->  X{$1}

但它失败了:

X(foo); Y(bar);   ->   X{foo); Y(bar};

有什么办法可以完成这种转变吗?

Is there any way to accomplish this transformation?

这个答案可能是关键吗?或者这个?

抱歉,我的示例列表不完整.提前分类会很困难,所以我只需要不断修改问题,直到我得到所有的边缘情况.我认为问题是任何一种伎俩都不能依靠.

Sorry, my list of examples was incomplete. It's going to be difficult to categorise in advance, so I will just have to keep amending the question until I've got all the edge cases. I think the problem is that any kind of trick cannot be relied upon.

推荐答案

这是因为运算符 * 和 + 是贪婪的.但是,您可以使用表达式

This is because the operators * and + are greedy. However, you can enforce laziness by using the expression

X\((.*?)\);

?Symbol 强制执行延迟匹配.

The ? Symbol enforces a lazy match.

这篇关于使用 REGEX 将 X(...) 替换为 X{...}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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