Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何在lua中查找方括号字符?_String_Lua_Pattern Matching - Fatal编程技术网

String 如何在lua中查找方括号字符?

String 如何在lua中查找方括号字符?,string,lua,pattern-matching,String,Lua,Pattern Matching,所以我试图在字符串中找到方括号: s = "testing [something] something else" x,y = string.find(s,"[") 这给了我一个错误:格式错误(缺少']') 我还尝试: x,y = string.find(s,"\[") 给我同样的错误 这是: x,y = string.find(s,"\\[") 在这种情况下,x和y为零 有没有关于如何正确地做到这一点的想法?提前感谢。使用第四个参数,以关闭模式匹配 x, y = string.find(

所以我试图在字符串中找到方括号:

s = "testing [something] something else"
x,y = string.find(s,"[")
这给了我一个错误:格式错误(缺少']')

我还尝试:

x,y = string.find(s,"\[")
给我同样的错误

这是:

x,y = string.find(s,"\\[")
在这种情况下,x和y为零


有没有关于如何正确地做到这一点的想法?提前感谢。

使用第四个参数,以关闭模式匹配

x, y = string.find(s, "[", nil, true)
约翰的答案是可行的——关闭模式匹配

您试图执行的操作(转义
[
)是使用Lua中的
%
字符完成的:

 x,y = string.find(s,'%[')
Lua中的字符串都有字符串模块作为其元表,因此您可以说:

 x,y = s:find('%[')
甚至:

 x,y = s:find'%['

这很好,谢谢。但是我现在要做的是找到这个模式:“[…]”意思是:一个括号,两个'。意思是任何字符,“:”,再次是2',和另一个括号。我应该把这个问题分成两个或更多的字符串吗?找到?谢谢。哦,Mud说:只要使用'%[“所以现在我可以使用“.”作为通配符。再次感谢您在这种情况下使用Mud的答案。您可以使用
%
符号转义
%
等字符;因此您可以使用
“%[…:%]”的模式。