i am saving filenames as an textfile in lua and want to search in the texfile afterwards for an filename. My problem is that i just get the searched string back, and not the whole filename.
this is my code:
local file,err = io.open("C:\\Users\\lamu7789\\Documents\\Lua_Plugins\\test_file_reader \\channels.txt", 'w')
if file then
for dir in io.popen([[dir "C:\Users\lamu7789\Documents\Lua_Plugins\test_file_reader\textfiles" /b]]):lines() do
file:write(dir.."\n")
end
file:close()
else
print("error: ", err)
end
channel = "0x"..string.upper("10")
local file = io.open("C:\\Users\\lamu7789\\Documents\\Lua_Plugins\\test_file_reader\\channels.txt", "rb")
if not file then return nil end
local String = file:read "*a"
local name = String:match(channel)
print(name)
file:close()
for this example i get "0x10" back. This is how the path looks like and whats "print(String)" prints out:
What i want to get back is like this: "0x10_adress_second.txt". What is wrong here? Thanks for your help.
CodePudding user response:
You need to match the full name like this:
channel = "0x10[_%w] .txt"
CodePudding user response:
string.match returns the captures. In your case that's "0x10"
.
If you want to capture the entire line you need to modify your pattern.
local s = "0x7E_address_first.txt\n0x10_address_second.txt\n"
print(s:match("0x7E[^\n]*"))
print(s:match("0x10[^\n]*"))
This will capture your starting characters followed by anything but a line break.
Please refer to https://www.lua.org/manual/5.4/manual.html#pdf-string.match