我应用了一个函数,但看起来很糟糕. function find_without_pattern(s1,s2) for i =1,#s1-#s2+1 do local t = string.sub(s1,i,#s2+i-1) if t == s2 then return i,i+#s2-1 end endend string.find方法提供了一个可选的第4个参数来
function find_without_pattern(s1,s2) for i =1,#s1-#s2+1 do local t = string.sub(s1,i,#s2+i-1) if t == s2 then return i,i+#s2-1 end end endstring.find方法提供了一个可选的第4个参数来自行强制执行 plaintext search.
例如:
string.find("he#.*o", "e#.*o", 1, true)
会给你正确的结果.
引用Lua手册页:
string.find (s, pattern [, init [, plain]])
A value of
true
as a fourth, optional argumentplain
turns off the pattern matching facilities, so the function does a plain “find substring” operation, with no characters in pattern being considered magic. Note that ifplain
is given, theninit
must be given as well.