Reputation: 53
I've been using Youtube API v3 (yes, I know, it wasn't meant for Lua) recently, but when I need to convert an ISO 8601 duration into a formatted string, nothing on the web helps. I've been googling all over the place, to search for a specific library that could help with this sort of thing, but unfortunately, there is NONE for Lua. There is thousands of libraries out there for other languages except Lua.
And now, it seems I'm stuck with string patterns which I don't even know how to use. So how else would I go about doing this task?
Example of an ISO 8601 duration:
PT3M33S
I want to convert it into something like this:
3:33
Upvotes: 2
Views: 475
Reputation: 72312
If you don't want to parse the whole ISO 8601 specification, try this code:
s="PT3M33S"
t=s:gsub("^.-(%d+)M(%d+)S","%1:%2")
print(t)
It uses Lua pattern matching. The pattern reads: skip everything until a run of digits followed by an M
and then find a run of digits followed by an S
. Capture both runs of digits and use them in the replacement pattern.
If you want to extract both numbers, use this:
s="PT3M33S"
M,S=s:match("^.-(%d+)M(%d+)S")
print(M,S)
Upvotes: 3