python - 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 split string list, not using 1 parameter 2 parameters.

  • first want capture number before l\\%(
  • second want capture text between \\%( , \\)\\| or in case of end of string between \\%( , \\)$

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 tried split string @ \\| , substituting every undesired character "".

is there better way in python?

one way approach use re.findall() 2 capturing groups find 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')] 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -