Use REGEX to replace X(...) with X{...}

1k views Asked by At

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

So:

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

Becomes

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

My IDE (XCode) supports RegEx search and replace.

I've tried:

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

But it fails on:

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

Is there any way to accomplish this transformation?

EDIT: might this answer hold the key? Or this one?

EDIT: 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.

1

There are 1 answers

4
Mido On

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

X\((.*?)\);

The ? Symbol enforces a lazy match.