Pattern in lua with anchors not matching

允我心安 提交于 2019-12-11 11:26:16

问题


Why this does not match? I want to match the exact pattern 2 letters followed by 3 numbers

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

回答1:


If you just want to see if a string matches a pattern, use string.match instead.

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

string.gmatch is for finding all matches in a string, and doesn't work correctly with ^ and $.



来源:https://stackoverflow.com/questions/26000428/pattern-in-lua-with-anchors-not-matching

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!