在引号之间提取subtring,但跳过\;并将其变成”而是在卢阿

发布于 2025-02-10 12:01:56 字数 1948 浏览 1 评论 0原文

我有这样的字符串

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海少女心 2025-02-17 12:01:56

该任务无法使用单个LUA模式完成,但可以通过几种模式来实现。
文本参数不得包含字节\ 0\ 1\ 2 - 这些特殊字符用于临时替代。

local function ExtractArgs(text)
   local arguments = {}
   for argument in 
      ('""'..text:gsub("\\?.", {['\\"']="\1"}))
      :gsub('"(.-)"([^"]*)', function(q,n) return "\2"..q..n:gsub("%s+", "\0") end)
      :sub(2)
      :gmatch"%Z+" 
   do
      argument = argument:gsub("\1", '"'):gsub("\2", ""):gsub("\\(.)", "%1")
      print(argument)
      arguments[#arguments+1] = argument
   end   
   return arguments
end

ExtractArgs[[argument \"\\ "" "some argument" "some argument with a quotation mark \" in here \\"]]

输出:

argument
"\

some argument
some argument with a quotation mark " in here \

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.

local function ExtractArgs(text)
   local arguments = {}
   for argument in 
      ('""'..text:gsub("\\?.", {['\\"']="\1"}))
      :gsub('"(.-)"([^"]*)', function(q,n) return "\2"..q..n:gsub("%s+", "\0") end)
      :sub(2)
      :gmatch"%Z+" 
   do
      argument = argument:gsub("\1", '"'):gsub("\2", ""):gsub("\\(.)", "%1")
      print(argument)
      arguments[#arguments+1] = argument
   end   
   return arguments
end

ExtractArgs[[argument \"\\ "" "some argument" "some argument with a quotation mark \" in here \\"]]

Output:

argument
"\

some argument
some argument with a quotation mark " in here \
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文