【问题标题】:Lua pattern to stop when end of lineLua 模式在行尾时停止
【发布时间】:2014-10-02 14:41:26
【问题描述】:

我需要帮助 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 输出脚本:

汤姆

密码=xyz

用户名=简

如何让模式在到达行尾后停止?

【问题讨论】:

    标签: string lua lua-patterns


    【解决方案1】:

    你可以使用模式

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

    请注意,除了额外的\n,使用惰性修饰符- 代替*


    正如@lhf 指出的那样,确保文件以新行结尾。我认为您可以在匹配之前手动将\n 附加到字符串。

    【讨论】:

    • 或者使用"(UserName=)([^\n]*)"甚至"(UserName=)(%w*)"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 2019-05-23
    • 2019-06-19
    • 2019-06-02
    • 2021-12-03
    • 2016-03-18
    相关资源
    最近更新 更多