Python regex – match and replace unknown length of elements
I have the following problem. Often data exists in a form:
IF X = (A OR B OR C OR ...)
... is a continuation of an often larger list. I would like to change it to:
IF X = 'A' OR X = 'B' OR X = 'C' OR ...
Where again, three dots are continuation. I have completely no idea how to solve it by regular expression or even how to ask properly about the solution.
How to do it?
You could use
(\w+)(?= OR|\)$) after removing
X = at the beginning:
(\w+) – match one or more of word-characters and store it inside capturing group,
(?= OR|\)$) – positive lookahead with alternation: assert what follows is
) and end of the string
Replace it with
X = \1, meaning replacing with
X = followed by first capturing group.