如何在python中拆分但忽略带引号和大括号的字符串中的分隔符?

时间:2019-06-06 09:16:08

标签: python string

我想在某些定界符上分割字符串,但不分割在其中存在的大括号字符串上的引号。说分号。

eg . '1;2;"3;4"; [5;6];7'

['1','2','"3;4"','[5;6]','7']

2 个答案:

答案 0 :(得分:2)

您可以使用如下正则表达式:

import re

str = '''1;2;"3;4"; [5;6];7'''
matcher = re.compile(r'''(\".+?\"|\[.+?\]|\(.+?\)|\{.+?\}|[^\"[({]+?)(?:;|$)''')

print(matcher.findall(str)) # returns ['1', '2', '"3;4"', '[5;6]', '7']

此正则表达式支持使用“,[,(,{和定界符;

答案 1 :(得分:0)

您也可以使用算法方式(最好使用monK_之类的正则表达式进行解析)

def splitter(string):
    ignoreStart = ["\"", "["]
    ignoreEnd = ["\"",  "]"]

    separator = ";"
    result = []
    buffer = ""
    specialCase = None
    for char in string:
        edited = False
        if not specialCase and char in ignoreStart:
            specialCase = ignoreEnd[ignoreStart.index(char)]
            edited = True

        if specialCase != None or char != separator:
            buffer += char
            if not edited and char == specialCase:
                specialCase = None
        else:
            result.append(buffer)
            buffer = ""
    if buffer != "":
        result.append(buffer)
    return result


print(splitter('1;2;"3;4";[5;6];7'))

输出:

['1', '2', '"3;4"', '[5;6]', '7']