如何在lua中查找方括号字符? [英] How to string.find the square bracket character in lua?

查看:490
本文介绍了如何在lua中查找方括号字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm trying to find square brackets inside a string:

s = "testing [something] something else"
x,y = string.find(s,"[")

这给了我一个错误:格式错误(缺少']').

which gives me an error: malformed pattern (missing ']').

我也尝试过:

x,y = string.find(s,"\[")

给我同样的错误.

这:

x,y = string.find(s,"\\[")

在这种情况下,x和y为零.

in this case x and y are nil.

关于如何正确执行此操作的任何想法?预先感谢.

Any thoughts on how to do this properly? Thanks in advance.

推荐答案

John的答案将起作用-关闭模式匹配.

John's answer will work -- turning off pattern matching.

您要执行的操作-退出[-是通过Lua中的%字符完成的:

What you're trying to do -- escape the [ -- is done with the % character in Lua:

 x,y = string.find(s,'%[')

Lua中的所有字符串都具有string模块作为其元表,因此您可以说:

Also strings in Lua all have the string module as their metatable, so you could just say:

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

甚至:

 x,y = s:find'%['

这篇关于如何在lua中查找方括号字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆