lua中的模式与锚不匹配 [英] Pattern in lua with anchors not matching

查看:117
本文介绍了lua中的模式与锚不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这不匹配?我想匹配2个字母,再加上3个数字的精确模式

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 

推荐答案

如果只想查看字符串是否与模式匹配,请改用string.match.

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用于查找字符串中的所有匹配项,并且不适用于^$.

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

这篇关于lua中的模式与锚不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆