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

前端 未结 13 1240
[愿得一人]
[愿得一人] 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:57

    If one wants to split string while keeping separators by regex without capturing group:

    def finditer_with_separators(regex, s):
        matches = []
        prev_end = 0
        for match in regex.finditer(s):
            match_start = match.start()
            if (prev_end != 0 or match_start > 0) and match_start != prev_end:
                matches.append(s[prev_end:match.start()])
            matches.append(match.group())
            prev_end = match.end()
        if prev_end < len(s):
            matches.append(s[prev_end:])
        return matches
    
    regex = re.compile(r"[\(\)]")
    matches = finditer_with_separators(regex, s)
    

    If one assumes that regex is wrapped up into capturing group:

    def split_with_separators(regex, s):
        matches = list(filter(None, regex.split(s)))
        return matches
    
    regex = re.compile(r"([\(\)])")
    matches = split_with_separators(regex, s)
    

    Both ways also will remove empty groups which are useless and annoying in most of the cases.

提交回复
热议问题