I have a string that contains variable names separated by 'and's/'or's such as 'x[1] and x[2] or x[3]'. The number of variable names varies as does whether it's an 'and' or 'or' that comes in between them. I want to wrap parenthesis around each stretch of variables separated by 'or's. For example, if the string is 'x[1] and x[2] or x[3] and x[4] or x[5] or x[6] and x[7]', I want to change it to 'x[1] and (x[2] or x[3]) and (x[4] or x[5] or x[6]) and x[7]'.
I'm not even a novice at regex. I was wondering if there is a fairly elegant and efficient way to do this using regex in python? Any help would be greatly appreacited.
Josh