python split string by multiple delimiters and/or combination of multiple delimiters


x = "121, 1238,nxyz,n 123abc nrabc123"

I want to split This string with the delimiters ",", "n", "r", "s" to get the output

['121', '1238', 'xyz', '123abc', 'abc123']

Whatever I try, the delimiters are accepted as single characters and not as combination of characters. e.g.


re.split("n|,|s|r", x)

Gave output of

['121', '', '1238', '', 'xyz', '', '', '123abc', '', '', 'abc123']

  1. re.split("ns|,|s|r", x)

Gave output of

['121', '', '1238', '', 'xyz', '', '123abc', '', 'abc123']

The second one is a slight improvement over the first one. But if that’s what is required, I need to give all possible combinations manually.
something Like (with more combinations):

re.split("ns|sn|sns|n|,s|s,|s,s|,|s|r", x)


['121', '1238', 'xyz', '', '123abc', '', 'abc123']

Is there any better way to do this?


Combining @Johnny Mopp‘s and @alfinkel24‘s comments:

re.split("[s,]+",  x)

Will split the string as required to

['121', '1238', 'xyz', '123abc', 'abc123']


  • [...] any of the characters.
  • + one or more repetitions of the previous characters.
  • s any white space characters including "n, r, t"

    Official documentation:

For Unicode (str) patterns: Matches Unicode whitespace characters (which includes [ tnrfv], and also many other characters, for example the non-breaking spaces mandated by typography rules in many languages). If the ASCII flag is used, only [ tnrfv] is matched.
For 8-bit (bytes) patterns: Matches characters considered whitespace in the ASCII character set; this is equivalent to [ tnrfv].