May-16-2023, 07:31 AM
An alternative to using
re.sub()
is re.finditer()
. This introduces an explicit loop instead of a callback function but the advantage is that the our_split()
function becomes a generator which is cleaner.import re def our_split(data): depth = 0 pos = -1 for match in re.finditer(r'[\[\],]', data): match match.group(0): case ',': if depth == 0: yield data[pos + 1 : (pos := match.start())] case '[': depth += 1 case ']': depth -= 1 yield data[pos + 1:] if __name__ == '__main__': data = "Richtung route, trend, way [route, direction], tendency [political etc.], course [direction], direction [course, route]" result = list(our_split(data)) print(result)