-4

I have the following problem. Often data exists in a form:

IF X = (A OR B OR C OR D)

or

IF X = (Aa OR Bb OR Cc)

where 'OR' are separators and letters are values - but, number of elements in list is undefined. I want to change it to:

IF X = 'A' OR X = 'B' OR X = 'C' OR 'D'

and, accordingly:

IF X = 'Aa OR X = 'Bb' OR X = 'Cc'

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?

Ch3shire
  • 1,095
  • 2
  • 14
  • 39

1 Answers1

4

You could use (\w+)(?= OR|\)$) after removing X = at the beginning:

Explanation:

(\w+) - match one or more of word-characters and store it inside capturing group,

(?= OR|\)$) - positive lookahead with alternation: assert what follows is OR or ) and end of the string

Replace it with X = \1, meaning replacing with X = followed by first capturing group.

Demo

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69