local symbols_trans = {
["\226\145\160"]--[[①]] = "\\item1",
["\226\145\161"]--[[②]] = "\\bananas",
["\226\145\162"]--[[③]] = "\\cactus",
["\226\145\163"]--[[④]] = "\\etc",
["\226\145\164"]--[[⑤]] = "\\item5",
["\226\145\165"]--[[⑥]] = "\\item6",
["\226\145\166"]--[[⑦]] = "\\item7",
["\226\145\167"]--[[⑧]] = "\\item8",
["\226\145\168"]--[[⑨]] = "\\item9",
["\226\145\169"]--[[⑩]] = "\\item10",
}
text = string.gsub(text, "(\266\145.)", symbol_trans)
或者,如果你想与"\\item"
全部更换:
text = string.gsub(text,
"\266\145[\160-\169]",
"\\item"
)
[\160-\169]
相当于[\160\161\162\163\164\165\166\167\168\169]
。 查看Lua manual了解范围以及一般情况下的Lua模式的信息。
你也可以是看中:
text = string.gsub(text,
"\266\145([\160-169])",
function(c)
return "\\item"..(string.byte(c)-160+1)
end
)
这将打开①
成\item1
,②
为\item2
,等等。
应该指出的是,Lua不处理Unicode。即使那些是UTF-8字符串,Lua也不会知道这一点。它会按原样查找每个UTF-8代码单元。原始代码工作的唯一原因是因为Lua正在寻找代码单元的*序列*。您在上面发布的内容将依次查找每个代码单元。 – 2012-04-08 01:48:14