Lua Patterns – 魔兽世界香草

我试图从游戏聊天中获取一些数据,但我无法弄清楚这种模式.

这是一个AddOn for the World of Warcraft Vanilla(私人服务器).

gsub功能:

http://wowprogramming.com/docs/api/gsub

http://wowwiki.wikia.com/wiki/API_gsub

我一直在用this解释,但现在有一个部分,我有这样的事情:

variable = gsub(string,"([%d+d]+)?...","")

我不知道模式应该是什么,因为字符串可以像下面的例子一样:

2d17h6m31s
1d8h31m40s
22h40m4s
8h6m57s
5m25s
37s

“([%d d])?”实际上是我的多次尝试.

我确实读过关于魔术字符()的内容. % – *? [^ $但仍有一些我不明白.如果我能得到简单的简历说明那就太棒了!

聊天的重要部分如下:

chat

编辑(ktb’s comment):

问题:我如何才能获得完整的“99d23h59m59s”(^(.* s)没有做到这一点)?

在99d23h59m59s中,99可以是1到99并且它总是在d之后,但是如果实际上是< number> d则它是可选的.然后相同于< number> h(数字的范围从1到24),< number> m(数字的范围从1到59).最后总会有一段时间.

更新:

/run for key in pairs(string)do ChatFrame1:AddMessage(key)end

使用该命令,我获得了string.functionName()的所有函数名称,这里是列表:

07005

07006

07007

07008

07009

string.dump()

070010

070011

070012

070013

070014

070015

信息更新:

Unlike several other scripting languages,Lua does not use POSIX regular expressions (regexp) for pattern matching. The main reason for this is size: A typical implementation of POSIX regexp takes more than 4,000 lines of code. This is bigger than all Lua standard libraries together. In comparison,the implementation of pattern matching in Lua has less than 500 lines. Of course,the pattern matching in Lua cannot do all that a full POSIX implementation does. Nevertheless,pattern matching in Lua is a powerful tool and includes some features that are difficult to match with standard POSIX implementations.

Source.

Unlike some other systems,in Lua a modifier can only be applied to a character class; there is no way to group patterns under a modifier. For instance,there is no pattern that matches an optional word (unless the word has only one letter). Usually you can circumvent this limitation using some of the advanced techniques that we will see later.

Source.

我在上面的引文中找不到“高级技术”.我只找到了this,我还不确定.

解决方法

几年前我用WoW插件遇到了相同的模式限制.它需要一些搜索,但我挖出了我的解析功能.

parse_duration.lua

--
-- string:parseDuration() - parse a pseudo ISO-8601 duration of the form
-- [nd][nh][nm][ns],where 'n' is the numerical value of the time unit and
-- suffix designates time unit as follows: 'd' - days,'h' - hours,-- 'm' - minutes,and,'s' - seconds. Unspecified time units have a value
-- of 0.
--

function string:parseDuration()
  local ts = {d=0,h=0,m=0,s=0}
  for v in self:lower():gfind("%d+[dhms]") do
    ts[v:sub(-1)] = tonumber(v:sub(1,-2))
  end

  return ts
end

以下测试您的样本数据.

duration_utest.lua

require "parse_duration"

local function main()
  local testSet = {
    "2d17h6m31s ago something happened","1d8h31m40s ago something happened","22h40m4s ago something happened","8h6m57s ago something happened","5m25s ago something happened","37s ago something happened","10d6s alias test 1d2h3m4s should not be parsed"
  }

  for i,testStr in ipairs(testSet) do
    -- Extract timestamp portion
    local tsPart = testStr:match("%S+")
    local ts = tsPart:parseDuration()

    io.write( tsPart," -> { ")
    for k,v in pairs(ts) do
      io.write(k,":",v," ")
   end
    io.write( "}\n" )
  end
end

main()

结果

2d17h6m31s -> { m:6 d:2 s:31 h:17 }
1d8h31m40s -> { m:31 d:1 s:40 h:8 }
22h40m4s -> { m:40 d:0 s:4 h:22 }
8h6m57s -> { m:6 d:0 s:57 h:8 }
5m25s -> { m:5 d:0 s:25 h:0 }
37s -> { m:0 d:0 s:37 h:0 }
10d6s -> { m:0 d:10 s:6 h:0 }

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...