Luckily, Python has this built-in :)
import re
# Regex pattern splits on substrings "; " and ", "
re.split('; |, ', string_to_split)
Update:
Following your comment:
>>> string_to_split = 'Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n', string_to_split)
['Beautiful', 'is', 'better', 'than', 'ugly']
Answer from Jonathan Livni on Stack OverflowLuckily, Python has this built-in :)
import re
# Regex pattern splits on substrings "; " and ", "
re.split('; |, ', string_to_split)
Update:
Following your comment:
>>> string_to_split = 'Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n', string_to_split)
['Beautiful', 'is', 'better', 'than', 'ugly']
Do a str.replace('; ', ', ') and then a str.split(', ')
I know that .split() will split the string into a list, with the separator being whatever I want.
But what do I do if I want to do it with more than 1 separator?
For example, if I have a string "ABC,DEF/GHI" and I want to split it into a list like ["ABC", "DEF","GHI"], how do I do it?
Hello. I'm working with user input and I'm looking for the most optimal way to split it with multiple delimiters. I will have for example this buffer: Hello, I am | a && human where | and && are delimiters. Not only do I need to split a string with these delimiters, I also need to store information about the delimiters: the order in which they appear in the initial buffer and also their corresponding delimiter type (so | or &&).
I attempted to use strtok() and while it does work for splitting the buffer using multiple delimiters I couldn't find a way to check which delimiter was used for tokenization.
Right now I'm thinking of just going through the buffer one char at a time and checking for delimiters that way which would allow me to see which delimiter it is and store the order in which they appear, but I'm unsure if this is the most efficient way of going through this.
Thanks