I’m beginning my adventure with regular expressions.
I’m interested in splitting specially formatted strings. If a letter is not inside parentheses it should become a different element of output list. Letters grouped inside parentheses should be put together.
Samples:
my string => wanted list
"ab(hpl)x"=>['a', 'b', 'hpl', 'x']"(pck)(kx)(sd)"=>['pck', 'kx', 'sd']"(kx)kxx(kd)"=>['kx', 'k', 'x', 'x', 'kd']"fghk"=>['f', 'g', 'h', 'k']
How can it be achieved with regular expressions and re.split?
Thanks in advance for your help.
This cannot be done with
re.split, as it would require splitting on zero length matches.From http://docs.python.org/library/re.html#re.split:
Here is an alternative:
And an example: