Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split up a string using 2 split parameters?

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.

  • First I want to capture the number before l\\%(
  • Second I want to capture the text between \\%( 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?

like image 228
Reman Avatar asked Feb 06 '23 04:02

Reman


2 Answers

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')]
like image 187
alecxe Avatar answered Feb 07 '23 18:02

alecxe


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']]
like image 39
Eric Duminil Avatar answered Feb 07 '23 17:02

Eric Duminil