开发者

Exclusive Pattern Matching In Lua

From this line

frame=    2 fps=  0 q=3.0 size=     开发者_运维知识库 -0kB time=00:00:13.33 bitrate=  -0.0kbits/s dup=0 drop=92

I only want to match the '2' in the frame. How do I accomplish this?


Here is the Lua pattern string to use:

"frame%=[%s]+(%d+)"

If you use this in a function like string.match, then this will capture the frame number as a string:

local framenumber = string.match(testString, "frame%=%s+(%d+)");

framenumber will either have the captured number (again, as a string. Convert it to a number with tonumber) or NIL if it was not found.


If frame is always at the beginning and there is always the same amount of spaces you don't actually need patterns. A simple string.sub will do:

local str = "frame=    2 fps= not really important"
local frame = tonumber(str:sub(7,12))


Something like this should work.

local data = 'frame=    2 fps=  0 q=3.0 size=      -0kB time=00:00:13.33 bitrate=  -0.0kbits/s dup=0 drop=92'
local _,_, frame = data:find("^frame%=%s+(%d+)")

if frame then
    print("Frame: "..frame)
end 


Given your comments, the simple pattern s:match("%d+") will work, but s:match("frame=%s*(%d+)") is probably more robust in the long run.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜