此错误是因为 string.find 使用 Lua Patterns。
大多数非字母数字字符,例如"[", ".", "-" 等都传达特殊含义。
string.find(fc,weird),或者更好,fc:find(weird) 正在尝试解析这些特殊字符,并出错。
不过,您可以使用这些模式来抵消其他模式。
weird = ("--[["):gsub("%W","%%%0") .. "\r?\n"
这有点令人生畏,但希望它是有意义的。
("--[[") 是你奇怪字符串的原始第一部分,按预期工作。
:gsub() 是一个用另一种模式替换模式的函数。再次,请参阅Patterns。
"%W" 是一个匹配所有不是字母、数字或下划线的字符串的模式。
%%%0 替换与自己匹配的所有内容(%0 是一个字符串,表示此匹配中的所有内容),跟在 % 之后,它被转义了。
所以这意味着[[会变成%[%[,这就是如何查找,以及类似模式'转义'的特殊字符。
\n 现在是\r?\n 的原因是指这些模式。如果它以 \n 结尾,则匹配它,就像以前一样。但是,如果它在 Windows 上运行,则换行符可能看起来像 \r\n。 (您可以阅读此HERE)。 ? 跟在一个字符之后,在这种情况下是 \r,意味着它可以可选地匹配它。所以这匹配--[[\n和--[[\r\n,同时支持windows和linux。
现在,当您运行 fc:find(weird) 时,它正在运行 fc:find("%-%-%[%[\r?\n"),这应该正是您想要的。
希望这有帮助!
如果你有点懒,完成代码
weird = ("--[["):gsub("%W","%%%0") .. "\r?\n" // Escape "--[[", add a newline. Used in our find.
// readAll(file)
// Takes a string as input representing a filename, returns the entire contents as a string.
function readAll(file)
local c = io.open(file, "rb") // Open the file specified by the argument. Read-only, binary (Doesn't autoformat things like \r\n)
local j = c:read("*all") // Dump the contents of the file into a string.
c:close() // Close the file, free up memory.
return j // Return the contents of the string.
end
// blockActive()
// returns whether or not the weird string was matched in 'functions.lua', executes 'blockDeactivated.lua' if it wasn't.
function blockActive()
local fc = readAll("functions.lua") // Dump the contents of 'functions.lua' into a string.
if fc:find(weird) then // If it functions.lua has the block-er.
require("blockDeactivated") // Require (Thus, execute, consider loadfile instead) 'blockDeactived.lua'
return false // Return false.
else
return true // Return true.
end
end
print(blockActive()) // Test? the blockActve code.