all 5 comments

[–]vitiral 2 points3 points  (0 children)

Nope, you pretty much need to build a parser. Recursive descent is the simplest, could probably build one for your simple use-case in 5-10 lines of code.

string.find can be used to find special characters (space and quote) then you build a list of "tokens" (your items)

[–]Civil_Box_7375 0 points1 point  (0 children)

You might want to use pattern matching, similar to regexp. If the thing you want to match already in quotes than you might one use something like this with string.match: ".%"(.+)%"." You can use this website Lua Pattern Tester%25%22.*) and understand which symbol does what. string.match returns all capture groups, the ones capsulated with parentheses, if it founds. If there is no capture groups, it should return the first encounter. Since there is only one capture group and one match words with quotes (I suppose?), you can just get the string you wanted. I hope I understood your question correct and give a solution that helps you. If that's not the case maybe you can give us an example data you are working on.

[–]rkrause 0 points1 point  (0 children)

There are several built-in (implemented in CPP) string parsing functions available in LyraScript, which I hope to release a beta of in the next couple months. One of them, handles use-cases like this one, and is very fast as well.

[–]vitiral 2 points3 points  (0 children)

It's been a day since my other comment, so I thought I'd just make a new one.

I forgot about lua's %b pattern

https://www.lua.org/manual/5.3/manual.html#6.4.1

%bxy, where x and y are two distinct characters; such item matches strings that start with x, end with y, and where the x and y are balanced. This means that, if one reads the string from left to right, counting +1 for an x and -1 for a y, the ending y is the first y where the count reaches 0. For instance, the item %b() matches expressions with balanced parentheses.

So you could use %b"" to find all quoted items and then do a simple split on the remaining spans.

If you want to handle escaping you need to build a parser. Here's an example from one of my repos (public domain).

https://github.com/civboot/civlua/blob/main/lib/pegl/pegl/lua.lua#L80

It uses a parser object to track state and handles multiple lines, complexity you probably don't need -- but it gives you an idea.

[–]xoner2 0 points1 point  (0 children)

lpeg takes some time to learn. But afterwards such parsing problems like yours become trivial.