I am having trouble coming up with a Lua 5.0 regular expression that will work for 2 scenarios.
1) expression = "[^V]Core %d+:%s*%+(%d+%.%d+)°C %(crit = %+(%d+%.%d+)°C%)"
This correctly matches this string:
Core 0: +45.0°C (crit = +100.0°C)
2) expression = "[^V]Core %d+:%s*%+(%d+%.%d+)°C %(high = %+%d+%.%d+°C, crit = %+(%d+%.%d+)°C%)"
This correctly matches this string:
Core 0: +45.0°C (high = +86.0°C, crit = +100.0°C)
However, I want to be able to match either string and have 2 captures: the first temperature and the critical temperature. (I don'开发者_开发知识库t need the high temperature). I tried this but no luck:
expression = "[^V]Core %d+:%s*%+(%d+%.%d+)°C %((?:high = %+%d+%.%d+°C, )crit = %+(%d+%.%d+)°C%)"
I am in Lua but I think the regex expression syntax closely matches other languages such as Perl. Anyone have any ideas?
The Lua string patterns are NOT regular expressions.
In order to do what you want - match two different strings - you need to actually try two matches.
local input = ... -- the input string
-- try the first pattern
local temp, crit = string.match(input, "[^V]Core %d+:%s*%+(%d+%.%d+)°C %(crit = %+(%d+%.%d+)°C%)"
-- if it didn't match, try the second match
if not temp then
temp, crit = string.match(input, "[^V]Core %d+:%s*%+(%d+%.%d+)°C %(high = %+%d+%.%d+°C, crit = %+(%d+%.%d+)°C%)")
end
if temp then
-- one of the two matches are saved in temp and crit
-- do something useful here
end
I think you need a ?
after the (?:...)
group.
There's something funny going on with the number of spaces before the parens as well -- the strings and the non-working regexp have two, while the 'working' regexps have one. I'd use %s+ there for robustness.
精彩评论