Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is :gmatch getting a piece of string that doesn't have a comma?

Asked by 5 years ago

I'm using this pattern: [^, ]+ Code:

function split(str)
    local strs = {}
    for someStr in str:gmatch("[^, ]+") do
        table.insert(strs, someStr)
    end
    return strs
end
print(unpack(split("123, lol, hi, oof, foo, bar, var")))

Seems okay right? But when I was testing, I pressed play, but missed a comma:

print(unpack(split("a, s d")))

And results?

a s d

The pattern is supposed to only get chunks of strings with a comma, but between s and d, there's no comma. I thought it'll be

a s

But apparently, no. Is there anything I can do to prevent that?

Answer this question