【发布时间】:2014-04-09 12:56:06
【问题描述】:
我知道我应该使用string.match() 来执行此操作,但我无法匹配字符串中“可能”的字符,例如:
teststring = "right_RT_12"
我可以轻松地做这样的事情:
string.match(teststring , 'righteye_RT_[0-9]+')
如果测试字符串的末尾总是有“_[0-9]+”,这很好,但可能会出现末尾没有数字的情况。我将如何在 Lua 中解决这个问题?
在 Python 中,我可以执行以下操作:
re.search("righteye_RT(_[0-9]+)?", teststring)
我认为这样的事情会起作用:
string.match(teststring, 'righteye_RT_?[0-9]+?')
但它没有。结果 = nil
但是,以下方法确实有效,但只能找到第一个数字:
string.match(teststring, 'righteye_RT_?[0-9]?')
【问题讨论】:
标签: string lua lua-patterns