lua中的魔法字符转义问题

        lua中的魔法字符有( ) . % + - * ? [ ] ^ $ ,在表示它们时候要考虑用到转义,转义符号为%,但有些却不需要转义。下面我们通过实际代码来测试一下。

       首先来认识一个函数,string.find()查找目标模板在给定字符串中出现的位置,找返回起始和结束位置,没找到返回nil

root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1(2","("))
root@16d2a5728582:~/mapan# lua test.lua 
lua: test.lua:1: unfinished capture
stack traceback:
	[C]: in function 'find'
	test.lua:1: in main chunk
	[C]: ?
root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1(2","%("))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# 
( 需要转义


root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1)2",")"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1)2","%)"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# 

) 不需要转义也能被find到,当然转义过了也没有问题


root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1.2","."))
root@16d2a5728582:~/mapan# lua test.lua 
1	1
root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1.2","%."))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# 

. 不需要转义能被find到,而且不会报错,但是返回的位置却不对,有点奇怪。加上转义字符%后,就成功返回了


root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1%2","%"))
root@16d2a5728582:~/mapan# lua test.lua 
lua: test.lua:1: malformed pattern (ends with '%')
stack traceback:
	[C]: in function 'find'
	test.lua:1: in main chunk
	[C]: ?
root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1%2","%%"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# 
 
% 不转义会被报错,加了转义字符之后就返回了正确的结果


root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1+2","+"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1+2","%+"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# 

+ 不转义不会报错,加上转义字符之后也没问题


root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1-2","-"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1-2","%-"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# 
-  不转义不会报错,加上转义字符也没有问题


root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1*2","*"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1*2","%*"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# 
* 不转义不会报错,加上转义字符也没问题

root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1?2","?"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1?2","%?"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan#  
? 不转义不会报错,加上转义字符也没问题

root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1[2","["))
root@16d2a5728582:~/mapan# lua test.lua 
lua: test.lua:1: malformed pattern (missing ']')
stack traceback:
	[C]: in function 'find'
	test.lua:1: in main chunk
	[C]: ?
root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1[2","%["))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# 

[ 不加转义会报错,加上转义之后正常


root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1]2","]"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1]2","%]"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# 
] 不加转义不会报错,加上转义之后也正常


root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1^2","^"))
root@16d2a5728582:~/mapan# lua test.lua 
1	0
root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1^2","%^"))
root@16d2a5728582:~/mapan# lua test.lua 
2	2
root@16d2a5728582:~/mapan# 
 
^ 不加转义前打印结果不正常,加上转义之后打印结果正常


root@16d2a5728582:~/mapan# cat test.lua 
print(string.find("1$2","$"))
root@16d2a5728582:~/mapan# lua test.lua 
4	3
root@16d2a5728582:~/mapan# 
$ 没加打印字符返回的结果不正常,加了之后打印结果就正常了


   这一些魔法字符,有些不需要转义,打印结果就正确;有些不转义,打印结果错误,但是不会报错;还有些直接报错。感觉挺有意思的,但是也有点不解,当然实际开发中,字符还是都应该加上转义。string.format()函数也会有类似的问题反馈


























你可能感兴趣的:(lua中的魔法字符转义问题)