Python split string without splitting escaped character

后端 未结 10 1380
谎友^
谎友^ 2020-12-08 20:56

Is there a way to split a string without splitting escaped character? For example, I have a string and want to split by \':\' and not by \'\\:\'

http\\://ww         


        
10条回答
  •  旧巷少年郎
    2020-12-08 21:26

    The edited version of Henry's answer with Python3 compatibility, tests and fix some issues:

    def split_unescape(s, delim, escape='\\', unescape=True):
        """
        >>> split_unescape('foo,bar', ',')
        ['foo', 'bar']
        >>> split_unescape('foo$,bar', ',', '$')
        ['foo,bar']
        >>> split_unescape('foo$$,bar', ',', '$', unescape=True)
        ['foo$', 'bar']
        >>> split_unescape('foo$$,bar', ',', '$', unescape=False)
        ['foo$$', 'bar']
        >>> split_unescape('foo$', ',', '$', unescape=True)
        ['foo$']
        """
        ret = []
        current = []
        itr = iter(s)
        for ch in itr:
            if ch == escape:
                try:
                    # skip the next character; it has been escaped!
                    if not unescape:
                        current.append(escape)
                    current.append(next(itr))
                except StopIteration:
                    if unescape:
                        current.append(escape)
            elif ch == delim:
                # split! (add current to the list and reset it)
                ret.append(''.join(current))
                current = []
            else:
                current.append(ch)
        ret.append(''.join(current))
        return ret
    

提交回复
热议问题