I need a pattern that will grab values for two or more parameters of varying length.
I need to convert a scanf format %i
to a lua pattern and it is proving very difficult. I don't need to worry about the type of storage that can be passed in with scanf. Just the %i
for integer and if they specify a specific link.
Documentation for scanf
, if needed, can be found here.
This is what I have so far:
if(scanfLetter == "i" or scanfLetter == "d" or scanfLetter == "u") then
if(specifiedNum == 0)then
newPattern = "([%+%-]?%d+)"
elseif(specifiedNum >= 2)then
newPattern = "([%+%-%d]?%d^".. string.rep("%d?", specifiedNum-2)..")$"
else
newPattern = "(%d)"
end
which basically just checks to see if they passed in a specific number or not (specifiedNum
). If it is 0 that means they didn't.
It all works until there are two length specified %i
s in a row. For example:
%5i%6i
If they enter in 6 or more characters it works fine because match will return two values: the first one consisting of the first 5 numbers and the 6th goes the next value.
The trouble is if there are fewer than 5
. In scanf
or printf
it will not match %5i%6i
if there are 5 or fewer numbers, but in my pattern it will still match under string.match and it returns all the values but the last in the first return and the second value get the last number entered.
More specific example so you don't have to type it all out and see it the pattern ends up looking like.
Given:
([%.%+%-%d]?%d[%.%d]?[%.%d]?[%.%d]?)([%.%+%-%d]?%d[%.%d]?[%.%d]?[%.%d]?[%.%d]?)
If 123456789
is passed to it, the match returns 2 values:
`12345` and `6789`
However, if 1234
is passed in, the match returns 2 values:
`123` and `4`
which is incorrect (it should not be a match).
Is what I seek possible?
(Maybe somebody has already written a scanf format to lua patterns converter?)