Example:
r="\\%4l\\%(wit.*wit\\)\\|\\%8l\\%(rood.*rood\\)\\|\\%12l\\%(blauw.*blauw\\)\\|\\%13l\\%(wit.*wit\\)\\|\\%14l\\%(blauw.*blauw\\)\\|\\%15l\\%(wit.*wit\\)\\|\\%16l\\%(wit.*wit\\)\\|\\%17l\\%(rood.*rood\\)\\|\\%19l\\%(wit.*wit\\)\\|\\%21l\\%(blauw.*blauw\\)"
I want to split the string to a list, but not using 1 parameter but 2 parameters.
l\\%(
\\%(
and \\)\\|
or
in case of the end of the string between \\%(
and \\)$
Output:
[[4, "wit.*wit"], [8, "rood.*rood"], [12, "blauw.*blauw"], [13, "wit.*wit"], [14, "blauw.*blauw"], [15, "wit.*wit"], [16,"wit.*wit"], [17, "rood.*rood"], [19, "wit.*wit"], [21, "blauw.*blauw"]]
What I tried is to split the string at \\|
and than substituting every undesired character with ""
.
Is there a better way to do this in Python?
One way to approach it would be to use re.findall()
with two capturing groups to find the desired pairs:
In [3]: re.findall(r"%(\d+)l\\%\((.*?)\\\)", r)
Out[3]:
[('4', 'wit.*wit'),
('8', 'rood.*rood'),
('12', 'blauw.*blauw'),
('13', 'wit.*wit'),
('14', 'blauw.*blauw'),
('15', 'wit.*wit'),
('16', 'wit.*wit'),
('17', 'rood.*rood'),
('19', 'wit.*wit'),
('21', 'blauw.*blauw')]
findall()
probably is the best solution.
Here's a relatively short way to write it with 2 splits and without substitution :
string = r"\%4l\%(wit.*wit\)\|\%8l\%(rood.*rood\)\|\%12l\%(blauw.*blauw\)\|\%13l\%(wit.*wit\)\|\%14l\%(blauw.*blauw\)\|\%15l\%(wit.*wit\)\|\%16l\%(wit.*wit\)\|\%17l\%(rood.*rood\)\|\%19l\%(wit.*wit\)\|\%21l\%(blauw.*blauw\)"
pairs = [substring[2:-2].split(r"l\%(") for substring in string.split(r"\|")]
# [['4', 'wit.*wit'], ['8', 'rood.*rood'], ['12', 'blauw.*blauw'], ['13', 'wit.*wit'], ['14', 'blauw.*blauw'], ['15', 'wit.*wit'], ['16', 'wit.*wit'], ['17', 'rood.*rood'], ['19', 'wit.*wit'], ['21', 'blauw.*blauw']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With