source:match("%d*")在lua中的含义是什么?

-- 解析速度值,以千米每小时为单位。
function Measure.parse_value_speed(source)
  local n = tonumber(source:match("%d*"))
  if n then
    if string.match(source, "mph") or string.match(source, "mp/h") then
      n = n * miles_to_kilometers
    end
    return n
  end
end

我对上面代码中的"*"感到困惑。任何意见都将不胜感激。

点赞
用户2858170
用户2858170

这个语法糖可以在 Lua 参考手册中找到!

source:match("%d*")

相当于 string.match(source, "%d*")

请看 https://www.lua.org/manual/5.3/manual.html#3.4.10

string.match(s, pattern [, init]) 在字符串 s 中查找第一个符合 pattern(参见 §6.4.1)的匹配项。成功则返回 pattern 中的匹配结果;否则返回 nil。若 pattern 未定义分组,则返回整个匹配结果。第三个可选的数字参数 init 指定开始查找的位置,其默认值为 1,可以为负数。

2019-11-08 10:54:01