Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: split string with delimiters from a list

I'd like to split a string with delimiters which are in a list.

The string has this pattern: Firstname, Lastname Email

The list of delimiters has this: [', ',' '] taken out of the pattern.

I'd like to split the string to get a list like this ['Firstname', 'Lastname', 'Email']

For a better understanding of my problem, this is what I'm trying to achieve:

The user shall be able to provide a source pattern: %Fn%, %Ln% %Mail% of data to be imported and a target pattern how the data shall be displayed:

%Ln%%Fn%; %Ln%, %Fn; %Mail%

This is my attempt:

data = "Firstname, Lastname Email"

for delimiter in source_pattern_delimiter:
    prog = re.compile(delimiter)
    data_tuple = prog.split(data)

How do I 'merge' the data_tuple list(s)?

like image 556
Murat G Avatar asked Sep 15 '25 15:09

Murat G


1 Answers

import re

re.split(re.compile("|".join([", ", " "])), "Firstname, Lastname Email")

hope it helps

like image 130
rodic Avatar answered Sep 17 '25 05:09

rodic