-3

I want to split a string on some delimiter but does not split quoted on braced string present in it. say semicolon.

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

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

2 Answers2

2

you can use regular expressions like so:

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']

This regex supports bracketing with ", [, (, { and the delimiter ;

monK_
  • 387
  • 1
  • 3
  • 10
0

You can also do it with the algorithmic way (it's far better do parse it with regular expression like 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'))

Output :

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

Xiidref
  • 1,456
  • 8
  • 20