In Python, how do I split a string and keep the separators?

前端 未结 13 1379
[愿得一人]
[愿得一人] 2020-11-22 03:26

Here\'s the simplest way to explain this. Here\'s what I\'m using:

re.split(\'\\W\', \'foo/bar spam\\neggs\')
-> [\'foo\', \'bar\', \'spam\', \'eggs\']
         


        
13条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 03:48

    You can also split a string with an array of strings instead of a regular expression, like this:

    def tokenizeString(aString, separators):
        #separators is an array of strings that are being used to split the string.
        #sort separators in order of descending length
        separators.sort(key=len)
        listToReturn = []
        i = 0
        while i < len(aString):
            theSeparator = ""
            for current in separators:
                if current == aString[i:i+len(current)]:
                    theSeparator = current
            if theSeparator != "":
                listToReturn += [theSeparator]
                i = i + len(theSeparator)
            else:
                if listToReturn == []:
                    listToReturn = [""]
                if(listToReturn[-1] in separators):
                    listToReturn += [""]
                listToReturn[-1] += aString[i]
                i += 1
        return listToReturn
        
    
    print(tokenizeString(aString = "\"\"\"hi\"\"\" hello + world += (1*2+3/5) '''hi'''", separators = ["'''", '+=', '+', "/", "*", "\\'", '\\"', "-=", "-", " ", '"""', "(", ")"]))
    

提交回复
热议问题