Lua pattern to stop when end of line

I need help for a pattern in Lua that stops to read after a line break. My code:

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

      

Script output using file2.ini:

Tom

Password = hug

UserName = Jane

How do I make the pattern stop after it reaches the end of the line?

+3


source to share


1 answer


You can use the template

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

      

Note that in addition to the extra , the lazy modifier is used \n

instead .*

-




As @lhf points out, make sure the file is newline-ending. I think you can add \n

to string manually before matching.

+4


source







All Articles