定位点不匹配的lua中的模式
为什么这不匹配?我想匹配精确的模式2个字母后接3个数字定位点不匹配的lua中的模式,lua,lua-patterns,Lua,Lua Patterns,为什么这不匹配?我想匹配精确的模式2个字母后接3个数字 s = "dd123" for w in string.gmatch(s, "^%a%a%d%d%d$") do print(w) matched = true end 如果您只想查看字符串是否与模式匹配,请改用string.match s = "dd123" print(string.match(s, "^%a%a%d%d%d$")) -- dd123 string.gmatch用于查找字
s = "dd123"
for w in string.gmatch(s, "^%a%a%d%d%d$") do
print(w)
matched = true
end
如果您只想查看字符串是否与模式匹配,请改用
string.match
s = "dd123"
print(string.match(s, "^%a%a%d%d%d$")) -- dd123
string.gmatch
用于查找字符串中的所有匹配项,但不能正确使用^
和$
您的预期输出是什么?单行程序有效print(string.match(“dd123”),“^%a%a%d%d%d$”)
这在regex
SO filtered标记中显示很有趣。@sln它有regex标记,但我编辑了它。为什么string.match
在for循环中失败?string.gmatch
不允许锚定到字符串的开头(这在中)。但我不确定为什么允许使用$
。