How could I split this string

lets say I have a string like this

"hey, {hey, {hey, hey}}, hey"

and I use

string.split(String, ",")

it will be

{
    hey,
    {hey,
    {hey,
    hey}},
    hey
}

instead I would want it to split like this

{
    hey,
    {hey, {hey, hey}},
    hey
{

should I use a for loop to achieve this or can I do still do this with string.split?

sorry if this is a bit confusing

I cant think of any way you could do this with string.split

I would personally use some kind of recursive function but Im sure theres another way

2 Likes

problem solved
ended up using a for loop to solve the issue I was having, it could probably be more efficient but let me try to put it here to where it would work in my example

local OldTable = string.split("hey, {hey, {hey, hey}}, hey", ",")
local MissingContainers = {0, 0}
local CombinedString = ""
local NewTable = {}

for i, v in ipairs(OldTable) do
	local _, Starts = string.gsub(v, "{", "")
	local _, Ends = string.gsub(v, "}", "")

	if Starts ~= Ends or MissingContainers[1] ~= MissingContainers[2] then
		MissingContainers[1] += Starts
		MissingContainers[2] += Ends
		CombinedString ..= (CombinedString ~= "" and "," or "")..v

		if MissingContainers[1] == MissingContainers[2] then
			table.insert(NewTable, CombinedString)
			CombinedString = ""
		end
	else
		table.insert(NewTable, v)
	end
end

print(OldTable, NewTable)

maybe someone can make it more efficient