1

这是我想要实现的目标:我想替换

void some_function(int,  int *, float);

void some_function(int a, int *b, float c);

到目前为止,当我遍历字符串时,我已经尝试用 chr(97+i) 替换“,”。

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

但这并没有起到作用。请让我知道是否有更好的方法来做到这一点。

4

3 回答 3

1
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);")
于 2012-06-07T17:39:06.757 回答
1
text = "void some_function(int,  int *, float);".replace(',', '%s,') % (' a', 'b')
于 2012-06-07T17:39:11.483 回答
0

原来的想法行不通,因为最后一个参数后面不是','而是')'。

import re
import string

def fix(text):
    n = len(re.findall('[,(]', text))
    return re.sub('([,)])', ' %s\\1', text) % tuple(string.letters[:n])
于 2012-06-07T17:51:31.810 回答