Lua:如何获取包含特定子字符串的字符串捕获?

在 Lua 中,我想从包含特定子字符串的字符串中获取捕获。例如在字符串中

test = "<item>foo</item>  <item>bar</item>"

我想要获取包含“a”的项,例如这里的“bar”。我尝试了这个:

print(string.find(test, "<item>(.-a.-)</item>"))

但结果是:

1   34  foo</item>  <item>bar

所以“.-”比我预想的更贪婪。什么是正确的模式?

点赞
用户107090
用户107090

尝试使用print(string.find(test, "<item>([^<]-a.-)</item>"))

2013-08-29 00:01:50