在lua中使用锚点的模式不匹配。

为什么这个匹配不成功?我想要匹配精确的模式——2个字母后面跟着3个数字。

   s = "dd123"
   for w in string.gmatch(s, "^%a%a%d%d%d$") do
      print(w)
      matched = true
    end
点赞
用户646619
用户646619

如果你只想确认一个字符串是否匹配一个模式,使用 string.match

s = "dd123"
print(string.match(s, "^%a%a%d%d%d$")) -- dd123

string.gmatch 用于在一个字符串中查找所有匹配项,但是它不支持 ^$

2014-09-23 16:51:34