简体   繁体   中英

Python regex and string replacement

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.

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.

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM