Reputation: 2587
I'm trying to convert strings in Python such as:
string = 'void demofun(double* output, double db4nsfy[], double VdSGV[], int length)'
into
wrapper = 'void demofun(ref double output, double[] db4nsfy, double[] VdSGV, int length)'
Now for most cases, I'm able to use a trivial combination of while
, string.find()
and string.replace()
to do this because I don't need to meddle with the variable names (such as output
or length
), but what I can't figure out is replacing these strings:
double db4nsfy[]
--> double[] db4nsfy
double[] VdSGV
--> double[] VdSGV
How should I do this? I know I will find my answer with some RTFM of regex in Python, but I'm hoping to start with a practical example.
Upvotes: 0
Views: 171
Reputation: 298532
You could use re.sub
:
>>> import re
>>> re.sub(r'(\w+) (\w+)\[\]', r'\1[] \2', string)
'void demofun(double* output, double[] db4nsfy, double[] VdSGV, int length)'
(\w+) (\w+)\[\]
matches two "words" wrapped in capturing groups and brackets.\1
and \2
refer to the stuff captured by those groups.Upvotes: 2
Reputation: 236
This is an intuitive approach without regex.
s = 'void demofun(double* output, double db4nsfy[], double VdSGV[], int length)'
s = s.split()
for i in range(len(s)):
if s[i][-3:] == '[],':
s[i] = s[i][:-3] + ','
s[i-1] = s[i-1] + '[]'
elif s[i][-3:] == '[])':
s[i] = s[i][:-3] + ')'
s[i-1] = s[i-1] + '[]'
s = ' '.join(s)
print s
# void demofun(double* output, double[] db4nsfy, double[] VdSGV, int length)
Upvotes: 1
Reputation: 11543
verbose, but without regex and handles both pointer and arrays(and also without regex):
def new_arguments(func_string):
def argument_replace(arguments):
new_arguments = []
for argument in arguments.split(', '):
typ, var = argument.split()
if typ.endswith('*'):
typ = 'ref ' + typ.replace('*', '')
if var.endswith('[]'):
var = var.replace('[]', '')
typ += '[]'
new_arguments.append(' '.join([typ, var]))
return ', '.join(new_arguments)
func_name = func_string[:func_string.index('(')]
arguments = func_string[func_string.index('(')+1:func_string.index(')')]
return ''.join((func_name, '(', argument_replace(arguments), ')'))
string = 'void demofun(double* output, double db4nsfy[], double VdSGV[], int length)'
print new_arguments(string)
#void demofun(ref double output, double[] db4nsfy, double[] VdSGV, int length)
Upvotes: 1