1

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.

Community
  • 1
  • 1
P i
  • 25,182
  • 33
  • 133
  • 229
  • As the language of balanced parentheses is not regular, I fear without "relying" on one trick or the other, your quest will not be solvable. – mikyra Nov 21 '14 at 01:09
  • XCode's regular expression (which likely uses NSRegularExpression and thus ICU's regular expression) doesn't have enough power to do bracket balancing. You should do the replacement with another text editor (for example Sublime Text). – nhahtdh Nov 21 '14 at 03:43

1 Answers1

3

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

X\((.*?)\);

The ? Symbol enforces a lazy match.

Mido
  • 645
  • 9
  • 20
  • 2
    won't this change `int z( (1+2)*(3+4) );` to `int z{ (1+2}*(3+4) );` ? – mikyra Nov 21 '14 at 00:57
  • Thank you, and +1. I've just come across situations this doesn't handle, and have amended/extended the question. – P i Nov 21 '14 at 01:04
  • If things get complicated, regular expressions will no longer have the enough powers and some coding work will have to be done to match the brackets and so on. You might just write a piece of code that does that instead of regular expressions. – Mido Nov 21 '14 at 01:16
  • If things get complicated... here is one solution: https://stackoverflow.com/q/53258096/1066234 – Avatar Nov 12 '18 at 10:13