Home > Software design >  multiple delimiters: split on first delimiter only if second is not found
multiple delimiters: split on first delimiter only if second is not found

Time:10-19

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.

CodePudding user response:

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']
  • Related