我將檔案名保存為 lua 中的文本檔案,然后想在 texfile 中搜索檔案名。我的問題是我只是取回了搜索到的字串,而不是整個檔案名。
這是我的代碼:
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()
對于這個例子,我得到了“0x10”。這是路徑的樣子,什么是“列印(字串)”列印出來的:

我想找回的是這樣的:“0x10_adress_second.txt”。這里有什么問題?謝謝你的幫助。
uj5u.com熱心網友回復:
您需要像這樣匹配全名:
channel = "0x10[_%w] .txt"
uj5u.com熱心網友回復:
string.match 回傳捕獲。在你的情況下,這是"0x10".
如果要捕獲整行,則需要修改模式。
local s = "0x7E_address_first.txt\n0x10_address_second.txt\n"
print(s:match("0x7E[^\n]*"))
print(s:match("0x10[^\n]*"))
這將捕獲您的起始字符,后跟除換行符以外的任何字符。
請參考https://www.lua.org/manual/5.4/manual.html#pdf-string.match
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318443.html
