0
for(int i=0; i<oldmethod(a, 7); i++) {
  sum += oldmethod(b*b+i-3, i%a);

---->

 for(int i=0; i<newmethod(7, 0, a); i++) {
  sum += newmethod(i%a, 0, b*b+i-3);

I had

sed 's/old(\([-a-zA-Z 0-9./*+]*\),\([-a-zA-Z 0-9./*+]*\))/new(\2, 0, \1)/'

But it only changes the first oldmethod, like

 for(int i=0; i<newmethod(7, 0, a); i++) {
   sum += oldmethod(b*b+i-3, i%a);
2
  • This looks like it would be safer to use an IDE to refactor this code. Using regular expressions for code is dodgy. Commented Nov 4, 2019 at 18:23
  • Why don't you just make oldmethod() call newmethod()?
    – Kusalananda
    Commented Nov 4, 2019 at 19:30

2 Answers 2

1

One of the arguments in your second line is i%a. That won't be matched by your regex because you haven't included % in your character class [-a-zA-Z 0-9./*+]. Try

sed 's/oldmethod(\([-a-zA-Z 0-9./*+%]*\),\([-a-zA-Z 0-9./*+%]*\))/newmethod(\2, 0, \1)/'

You may find further instances using other characters you've not considered, such as _ in variable names, or ( and ) in nested calls to other functions. You'll have to keep extending the subexpression as you encounter such uses.

1
  • Perhaps simplify: [^,]* instead of [lots of characters]* Commented Nov 4, 2019 at 18:26
0

A more generic possibility:

sed -E 's/old([^,]*\()([^,])*, ([^)]*)\);/new\1\3, 0, \2);/' file

Output:

for(int i=0; i<newmethod(7, 0, a); i++) {
  sum += newmethod(i%a, 0, b*b+i-3);
3
  • 1
    You'll want to use less greedy elements: [^,]* instead of .* Commented Nov 4, 2019 at 18:25
  • @glennjackman edite the question, thanks. IS ok now? Commented Nov 4, 2019 at 18:34
  • Now that I look at it again, the 3rd group should be non-close-parenthesis Commented Nov 4, 2019 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .