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