Reputation: 339
Here is what I am trying to achieve: I want to replace
void some_function(int, int *, float);
with
void some_function(int a, int *b, float c);
So far, I have tried to replace "," with chr(97+i) as I loop through the string.
text = len("void some_function(int, int *, float);")
i = 0
for j in range(0, length)
if text[j] == ",":
rep = " " + chr(97+i) + " .,"
text = text.replace("," rep)
i = i + 1
j = 0 # resetting the index, but this time I want to find the next occurrence of "," which I am not sure
But this is not doing the job. Please let me know if there is a better way of doing this.
Upvotes: 1
Views: 2680
Reputation: 8189
The original idea does not work, since the last parameter is not followed by a ',' but by a ')'.
import re
import string
def fix(text):
n = len(re.findall('[,(]', text))
return re.sub('([,)])', ' %s\\1', text) % tuple(string.letters[:n])
Upvotes: 0
Reputation: 2491
text = "void some_function(int, int *, float);".replace(',', '%s,') % (' a', 'b')
Upvotes: 1
Reputation: 97571
import re
i = 96
def replace(matches):
global i
i += 1
return " " + chr(i) + matches.group(0)
re.sub(',|\)', replace, "void some_function(int, int *, float);")
Upvotes: 1