Lua模式匹配不起作用。

我在测试脚本中遇到了问题

names.txt的内容:
foo 1
test 0

data="names.txt"
name="test"
--
d=io.open(data,"r")
s=d:read("*a")
f=string.gsub(s,"%"..name,"%1 1")
print(f)
print"------"
print(f:gsub("(%w+)%s*(%d)","%1"):format("%s"))

在lua上的输出为

foo 1
test 1 0
------
foo
test 0

我希望使用 string 来获取第一个数字

test 0 到 test 1

希望有人可以帮我。

点赞
用户204011
用户204011

你想要做什么?

data="names.txt"
name="test"
--
d=io.open(data,"r")
s=d:read("*a")
f=string.gsub(s,"(" .. name .. ")%s+%d+","%1 1")
print(f)

结果:

foo 1
test 1

如果不是,请更加具体地描述你想要的输出结果。

2013-10-22 09:26:50