例如,我有
str = "Beamer-Template!navigation symbols@\\texttt {navigation symbols}"
print(str:gsub('[^!|@%s]+@', ''))
可打印
Beamer-Template!navigation \texttt {navigation symbols}
但应该是
Beamer-Template!\texttt {navigation symbols}
我如何抓住空间?
重要的只是foo@bar
。该模式适用于像
str="foo@bar!baz@foobar!nice|crazy"
-> bar!foobar!nice|crazy
但没有额外的空格
str="foo@bar!baz baz@foobar!nice|crazy"
-> bar!baz foobar!nice|crazy
应为bar!foobar!nice|crazy
答案 0 :(得分:1)
要匹配makeindex
条目,使用LPEG语法可能会很有用。这样,您可以在分隔符处进行拆分,甚至根据匹配的字段执行语义操作。
local lpeg = assert(require"lpeg")
local C, S = lpeg.C, lpeg.S
local sep = S("@!|")
local str = C((1 - sep)^0)
local idx = str * ( "@" * str / function(match) return "@" .. match end
+ "!" * str / function(match) return "!" .. match end
+ "|" * str / function(match) return "|" .. match end)^0
print(idx:match("hello!world@foo|bar"))
$ lua test.lua
hello !world @foo |bar
回答评论:在表中收集匹配项。匹配项将根据其前缀进行收集。
local lpeg = assert(require"lpeg")
local C, Ct, S = lpeg.C, lpeg.Ct, lpeg.S
local sep = S("@!|")
local str = C((1 - sep)^0)
local match = function(expr)
local prefix = function(prefix)
return function(match)
return prefix .. match
end
end
local idx = str * ( "@" * str / prefix("@")
+ "!" * str / prefix("!")
+ "|" * str / prefix("|"))^0
return Ct(idx):match(expr)
end
for _, str in ipairs{
"hello!world@foo|bar",
"foo@bar!baz baz@foobar!nice|crazy",
"foo@bar!baz@foobar!nice|crazy",
"Beamer-Template!navigation symbols@\\texttt {navigation symbols}"
} do
local t = match(str)
print(table.concat(t," "))
end
$ lua test.lua
hello !world @foo |bar
foo @bar !baz baz @foobar !nice |crazy
foo @bar !baz @foobar !nice |crazy
Beamer-Template !navigation symbols @\texttt {navigation symbols}