String.split spaces which are only outside brackets

The issue is simple and I know it is possible but I cant deal with strings, I just don’t get them.
I want to split strings with spaces but I need to check if the spaces are not in brackets.
Willing to use other stuff besides string.split because that seems reasonable.

local str: string = "this is    test (ff, gg)"
str = string.gsub(str, "%s+", " ") -- //"this is    test (ff, gg)" = "this is test (ff, gg)"

local tabl = string.split(str, " ")
-- //String will be split like: {"this", "is", "test", "(ff," , "gg)"}
-- //Instead of: {"(ff," , "gg)"}
-- //I want: {"(ff, gg)"}
2 Likes

Im not sure what you mean, could you provide an example please?

1 Like

added the example, will try to elaborate further

1 Like

You can use a regular expression to match the string you want:
local str = “this is test (ff, gg)”

local t = {}
for i in string.gmatch(str, “%(([^%)]+)%)”) do
t[#t+1] = i
end

for i=1,#t do
print(t[i])
end

I tried this on a random Lua interpreter and it works.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.