Split Strings into words with multiple word boundary delimiters

前端 未结 30 2622
既然无缘
既然无缘 2020-11-21 05:09

I think what I want to do is a fairly common task but I\'ve found no reference on the web. I have text with punctuation, and I want a list of the words.

\"H         


        
30条回答
  •  孤街浪徒
    2020-11-21 06:10

    I like pprzemek's solution because it does not assume that the delimiters are single characters and it doesn't try to leverage a regex (which would not work well if the number of separators got to be crazy long).

    Here's a more readable version of the above solution for clarity:

    def split_string_on_multiple_separators(input_string, separators):
        buffer = [input_string]
        for sep in separators:
            strings = buffer
            buffer = []  # reset the buffer
            for s in strings:
                buffer = buffer + s.split(sep)
    
        return buffer
    

提交回复
热议问题