在引号之间提取subtring,但跳过\;并将其变成”而是在卢阿
我有这样的字符串
"argument \\\" \"some argument\" \"some argument with a quotation mark \\\" in here \""
,它可以打印出来
argument \" "some argument" "some argument with a quotation mark \" in here"
,并且我正在尝试提取所有字符串,以便最后存储这样的存储:
> [1] = "argument",
> [2] = """,
> [3] = "some argument",
> [4] = "some argument with a quotation mark " in here"
这是我到目前为止的代码。
function ExtractArgs(text)
local skip = 0
local arguments = {}
local curString = ""
for i = 1, text:len() do
if (i <= skip) then continue end
local c = text:sub(i, i)
if (c == "\\") and (text:sub(i+1, i+1) == "\"") then
continue
end
if (c == "\"") and (text:sub(i-1, i-1) ~= "\\") then
local match = text:sub(i):match("%b\"\"")
if (match) and (match:sub(#match-1,#match-1) ~= "\\") then
curString = ""
skip = i + #match
arguments[#arguments + 1] = match:sub(2, -2)
else
curString = curString..c
end
elseif (c == " " and curString ~= "") then
arguments[#arguments + 1] = curString
curString = ""
else
if (c == " " and curString == "") then
continue
end
curString = curString..c
end
end
if (curString ~= "") then
arguments[#arguments + 1] = curString
end
return arguments
end
print(ExtractArgs("argument \\\" \"some argument\" \"some argument with a quotation mark \\\" in here\""))
的报价标记之间不是,但不是在引号之间
它正确提取\“
正确
。 “ \\]*(?:\\。[^\“ \\]*)*)\” ,但是lua呢?
I have this string
"argument \\\" \"some argument\" \"some argument with a quotation mark \\\" in here \""
which prints out as this
argument \" "some argument" "some argument with a quotation mark \" in here"
and I am trying to extract all of it, so that at the end it gets stored like this:
> [1] = "argument",
> [2] = """,
> [3] = "some argument",
> [4] = "some argument with a quotation mark " in here"
This is the code that I have so far.
function ExtractArgs(text)
local skip = 0
local arguments = {}
local curString = ""
for i = 1, text:len() do
if (i <= skip) then continue end
local c = text:sub(i, i)
if (c == "\\") and (text:sub(i+1, i+1) == "\"") then
continue
end
if (c == "\"") and (text:sub(i-1, i-1) ~= "\\") then
local match = text:sub(i):match("%b\"\"")
if (match) and (match:sub(#match-1,#match-1) ~= "\\") then
curString = ""
skip = i + #match
arguments[#arguments + 1] = match:sub(2, -2)
else
curString = curString..c
end
elseif (c == " " and curString ~= "") then
arguments[#arguments + 1] = curString
curString = ""
else
if (c == " " and curString == "") then
continue
end
curString = curString..c
end
end
if (curString ~= "") then
arguments[#arguments + 1] = curString
end
return arguments
end
print(ExtractArgs("argument \\\" \"some argument\" \"some argument with a quotation mark \\\" in here\""))
It extracts \"
correctly that is not inbetween quotation marks, but not if it is inbetween quotation marks.
How can this be solved properly?
This seems to work with regex \"([^\"\\]*(?:\\.[^\"\\]*)*)\"
but what about Lua?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该任务无法使用单个LUA模式完成,但可以通过几种模式来实现。
文本
参数不得包含字节\ 0
,\ 1
和\ 2
- 这些特殊字符用于临时替代。输出:
The task cannot be done with a single Lua pattern but can be achieved with a chain of a few patterns.
The
text
parameter must not contain bytes\0
,\1
and\2
- these special characters are used for temporary substitution.Output: