multiple delimiters: split on first delimiter only if second is not found

Question:

Trying to create a regular expression that will split a string on either pipe (|) or comma (,) only if the string does not contain the pipe character.

So i know this will split on both delimiters fine:

>>> s = '10,20|30|40'
>>> re.split(',||', s)

['10', '20', '30', '40']

but what i really want in this case is to only split on the pipe, so the results should be:

['10,20', '30', '40']

not exactly sure how to do this with regular expression. i have tried something like this:

re.split('[,![^(|.*)]||]', s)

but that just splits again by both delimiters.

Asked By: mike01010

||

Answers:

If you can use the python regex module, you can use variable length lookarounds to split on a comma, only when there isn’t a | in the string:

import regex

strs = ['10,20|30|40', '10|20,30|40', '10,20,30', '10|20,30,40', '10|20|30']
for s in strs:
    print(regex.split(r'(?<!|.*),(?!.*|)||', s))

Output:

['10,20', '30', '40']
['10', '20,30', '40']
['10', '20', '30']
['10', '20,30,40']
['10', '20', '30']
Answered By: Nick
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.