Regular expressions are a language inside a language -
So, while it would be certainly possible to do what you want with a regular expression, it is not worth it. If for nothing else, you loose readability and maintainability, besides having to think a lot more just to get it working in first placing.
It can safely be donei n Python, without resort to the use of regex, in a way you have full control of where your new parameter is added.
The code bellow can do that, provided no calls to func
are broken in more than one line of source code:
add_parm_func = "func"
parm_to_add = "newparam"
def change(line):
start = line.find(add_parm_func) + len(add_parm_func)
res = line[:start]
open_paren = 0
for i, chr in enumerate(line[start:])
if chr == "(":
open_paren += 1
elif chr == ")":
open_paren -= 1
if open_paren == 0:
res += ", %s )" % parm_to_add
break
res += chr
res += line[start + i:]
return res
with open("sourcefile.c") as src, open("destfile.c") as dst:
for line in src:
if add_parm_func in line:
line = change(line)
dst.write(line)