Regex help...pretty please?

vbgunz vbgunz at gmail.com
Wed Aug 23 22:43:25 EDT 2006


MooMaster Wrote:
> I'm trying to develop a little script that does some string
> manipulation. I have some few hundred strings that currently look like
> this:
> cond(a,b,c)
> and I want them to look like this:
> cond(c,a,b)

I zoned out on your question and created a very simple flipper.
Although it will not solve your problem maybe someone looking for a
simpler version may find it useful as a starting point. I hope it
proves useful. I'll post my simple flipper here:

s = 'cond(1,savv(grave(3,2,1),y,x),maxx(c,b,a),0)'
def argFlipper(s):
    ''' take a string of arguments and reverse'em e.g.
    >>> cond(1,savv(grave(3,2,1),y,x),maxx(c,b,a),0)
     -> cond(0,maxx(a,b,c),savv(x,y,grave(1,2,3)),1)

    '''

    count = 0
    keyholder = {}
    while 1:
        if s.find('(') > 0:
            count += 1
            value = '%sph' + '%d' % count
            tempstring = [x for x in s]
            startindex = s.rfind('(')
            limitindex = s.find(')', startindex)
            argtarget = s[startindex + 1:limitindex].split(',')
            argreversed = ','.join(reversed(argtarget))
            keyholder[value] = '(' + argreversed + ')'
            tempstring[startindex:limitindex + 1] = value
            s = ''.join(tempstring)
        else:
            while count and keyholder:
                s = s.replace(value, keyholder[value])
                count -= 1
                value = '%sph' + '%d' % count
            return s  

print argFlipper(s)




More information about the Python-list mailing list