Let's say I have lines of the form:
int[4] height
char c
char[50] userName
char[50+foo("bar")] userSchool
As you can see, the expression in parentheses is optional.
Can I parse these lines using Lua string.match()?
The following figure works for strings containing brackets:
line = "int[4] height"
print(line:match('^(%w+)(%b[])%s+(%w+)$'))
But is there a pattern that can handle additional brackets as well? The following is not work :
line = "char c"
print(line:match('^(%w+)(%b[]?)%s+(%w+)$'))
Is it possible to write a template in another way to solve this problem?
source
share