Lua模式,以在行末停止

我需要获取 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

使用 file2.ini 运行脚本的输出:

Tom

Password=xyz

UserName=Jane

如何让这个模式在达到行末时停止匹配?

点赞
用户1009479
用户1009479

你可以使用下面的模式:

"(UserName=)(.-)\n"

注意,除了额外的 \n 外,懒惰修饰符 - 用于替代 *


正如 @lhf 指出的那样,确保文件以换行符结束。我认为你可以在匹配前手动追加一个 \n 到字符串中。

2014-10-02 14:47:04