Pek*_*ese 3 string lua lua-patterns
我需要获得关于 Lua 中的一种模式的帮助,该模式在换行后停止读取。我的代码:
function getusers(file)
local list, close = {}
local user, value = string.match(file,"(UserName=)(.*)")
print(value)
f:close()
end
f = assert(io.open('file2.ini', "r"))
local t = f:read("*all")
getusers(t)
--file2.ini--
user=a
UserName=Tom
Password=xyz
UserName=Jane
Run Code Online (Sandbox Code Playgroud)
使用 file2.ini 的脚本输出:
汤姆
密码=xyz
用户名=Jane
如何让模式在到达行尾后停止?
您可以使用该模式
"(UserName=)(.-)\n"
Run Code Online (Sandbox Code Playgroud)
请注意,除了 extra 之外,还使用\nlazy 修饰符来代替。-*
正如 @lhf 指出的,确保文件以新行结尾。我认为您可以\n在匹配之前手动将 a 添加到字符串中。
| 归档时间: |
|
| 查看次数: |
2157 次 |
| 最近记录: |