I have a csv file that has data like this:
+12345678901,08:00:00,12:00:00,1111100,35703,test.domain.net
+12345678901,,,0000000,212,test.domain.net
I'm trying to write lua code that will loop through each line, and create an array of values like this:
local mylist = {}
for line in io.lines("data/dd.csv") do
local id, start, finish, dow, int, domain = line:match("(+%d+),(%d*:*),(%d*:*),(%d*),(%d*),(%a*.*)")
mylist[#mylist + 1] = { id = id, start = start, finish = finish, dow = dow, int = int, domain = domain}
print(mylist[#mylist]['id'])
end
The problem is that when the code hits a line that has empty values for start and finish, the regex fails and all fields are nil.
I thought using the * meant 0 or more... I can't seem to find my error / typo.
Thanks.
This pattern works for me:
"(.-),(.-),(.-),(.-),(.-),(.-)$"
It seems that you just need to group the digits and : inside a [...]:
match("(%+%d+),([%d:]*),([%d:]*),(%d*),(%d*),(.*)")
^ ^^^^^^ ^^^^^^
Now, the [%d:]* matches zero or more digits or : symbols. Your pattern did not find the match because %d*:* matched 0+ digits followed with 0+ : symbols, and you had more than 1 such sequence.
Also, you need to escape the first + to make sure it matches a literal +.
See online Lua demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With