Python regex string expansion
python, regex, string
Solution
Here's a pretty clean way. You'll have fun figuring out how it works :-)
def expander(s):
import re
from itertools import product
pat = r"\(([^)]*)\)"
pieces = re.split(pat, s)
pieces = [piece.split("|") for piece in pieces]
for p in product(*pieces):
yield "".join(p)
Then:
for s in ('(A|B|C)_STRING',
'(|A_)STRING',
'STRING_(A|B)_STRING_(C|D)'):
print s, "->"
for t in expander(s):
print " ", t
displays:
(A|B|C)_STRING ->
A_STRING
B_STRING
C_STRING
(|A_)STRING ->
STRING
A_STRING
STRING_(A|B)_STRING_(C|D) ->
STRING_A_STRING_C
STRING_A_STRING_D
STRING_B_STRING_C
STRING_B_STRING_D
Problem
Suppose I have the following string: ``` trend = '(A|B|C)_STRING' ``` I want to expand this to: ``` A_STRING B_STRING C_STRING ``` The OR condition can be anywhere in the string. i.e `STRING_(A|B)_STRING_(C|D)` would expand to ``` STRING_A_STRING_C STRING_B_STRING C STRING_A_STRING_D STRING_B_STRING_D ``` I also want to cover the case of an empty conditional: `(|A_)STRING` would expand to: ``` A_STRING STRING ``` Here's what I've tried so far: ``` def expandOr(trend): parenBegin = trend.index('(') + 1 parenEnd = trend.index(')') orExpression = trend[parenBegin:parenEnd] originalTrend = trend[0:parenBegin - 1] expandedOrList = [] for oe in orExpression.split("|"): expandedOrList.append(originalTrend + oe) ``` But this is obviously not working. Is there any easy way to do this using regex?