You are not logged in.

#1 2008-11-18 09:05:18

freigeist
Member
From: Cologne, Germany
Registered: 2006-07-14
Posts: 191

Lua / string.gmatch

Maybe I'm just too stupid to get it. Im trying to parse a string (for an battery widget in awesome):

The string I want to parse is something like that:

Battery 1: discharging, 46, 02:03:04 remaining

I'm just interested in the last part (02:03:04 remaining), so I tried the following:

for s in string.gmatch(output, "(%w+) remaining))

But I only get the last two digits (04). I think that gmatch treats the : as a seperator and therefore 04 already fulfills the regular expression. What can I do now?

Last edited by freigeist (2008-11-18 09:21:37)


Elfenbeinturm.cc
a metaphysical space of solitude and sanctity: http://www.elfenbeinturm.cc

Offline

#2 2008-11-18 09:36:47

freigeist
Member
From: Cologne, Germany
Registered: 2006-07-14
Posts: 191

Re: Lua / string.gmatch

Solved it...but I'm not satisfied, there must be an more elegant way to do this

for s in string.gmatch(output, "(%d:%d+)%:") do
    s2 = s2..s..':'
end
s2=string.sub(s2,1,string.len(s2)-1)
return {s2..' h'}

Last edited by freigeist (2008-11-18 09:38:03)


Elfenbeinturm.cc
a metaphysical space of solitude and sanctity: http://www.elfenbeinturm.cc

Offline

#3 2008-11-18 10:23:00

chimeric
Member
From: Munich, Germany
Registered: 2007-10-07
Posts: 254
Website

Re: Lua / string.gmatch

Do you get the string by executing a shell command? If so, why not use sth. like cut to extract the relevant bits on the fly?

I.e. I use the following:

local f = io.popen("acpi -b | cut -d, -f2-3")

Offline

#4 2008-11-18 23:42:02

miko
Member
From: Poland
Registered: 2006-04-16
Posts: 49

Re: Lua / string.gmatch

freigeist wrote:

I'm just interested in the last part (02:03:04 remaining), so I tried the following:

for s in string.gmatch(output, "(%w+) remaining))

But I only get the last two digits (04). I think that gmatch treats the : as a seperator and therefore 04 already fulfills the regular expression. What can I do now?

Try this:
for s in output:gmatch('([%w:]+) remaining')

Offline

Board footer

Powered by FluxBB