문제

I have an external application which monitors CPU and GPU temperatures...

I am using Lua with the alien extension to grab these values (via GetWindowText) and to do some pattern matching on these values, effectively extracting the temperature digits out of the string, which by default shows up as something like CPU 67.875 °C...
But perhaps I have the wrong idea on how patterns work in LUA (since they don't appear to be exactly like regex)?

The pattern I am using is [%d]+[.%d+]* which should match any number between 0 and 100.0, correct?
Yet oddly enough, I am getting incredibly strange output when values reach around 56.5 degrees (see link).

Why is this happening?
And how can I extract the correct floating point values (as a string) between 0 and 100 in the format of XYY.ZZZ, where X is not optional, Y is optional, and . is optional unless Z exists?

도움이 되었습니까?

해결책

You're seeing the effect of accumulated rounding errors because 0.16 cannot be precisely represented in floating point. The code below performs better:

local n = 0
while n < 10000 do
    local s = tostring(n/100)
    local t = s:match("[%d]+[.%d+]*")
    print(t)
    n = n + 16
end

Now, to your question, try the simpler pattern below:

s="CPU 67.875 °C"
print(s:match("CPU +(.-) +"))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top