I use the regex [,;\s]+ to split a comma, space, or semicolon separated string. This works fine if the string doesn't have a comma at the end:
>>> p=re.compile('[,;\s]+')
>>> mystring='a,,b,c'
>>> p.split(mystring)
['a', 'b', 'c']
When the string has a comma at the end:
>>> mystring='a,,b,c,'
>>> p.split(mystring)
['a', 'b', 'c', '']
I want the output in this case to be ['a', 'b', 'c'].
Any suggestions on the regex?
Try:
str = 'a,,b,c,'
re.findall(r'[^,;\s]+', str)
Here's something very low tech that should still work:
mystring='a,,b,c'
for delim in ',;':
mystring = mystring.replace(delim, ' ')
results = mystring.split()
PS:
While regexes are very useful, I would strongly suggest thinking twice about whether it is the right tool for the job here. While I'm not sure what the exact runtime of a compiled regex is (I'm thinking at most O(n^2)), it is definitely not faster than O(n), which is the runtime of string.replace
. So unless there is a different reason for which you need to use a regex, you should be set with this solution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With