Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
动态url路径的Lua模式匹配_Lua_Pattern Matching_Lua Patterns - Fatal编程技术网

动态url路径的Lua模式匹配

动态url路径的Lua模式匹配,lua,pattern-matching,lua-patterns,Lua,Pattern Matching,Lua Patterns,我需要更改和存储传入请求的动态url路径,并将其存储在后端 下面是示例url GET /v1/merchants 用我的Lua代码我把它改成 GET_/v1/merchants 一切都很好,正是我所需要的。但当我尝试在url中使用动态路径时,问题就出现了 例如:GET/v1/content/merchants/{string} url可以是GET/v1/content/merchants/foo或GET/v1/content/merchants/bar 我想在后端存储上述格式,如GET/v

我需要更改和存储传入请求的动态url路径,并将其存储在后端

下面是示例url

GET /v1/merchants 
用我的Lua代码我把它改成

GET_/v1/merchants
一切都很好,正是我所需要的。但当我尝试在url中使用动态路径时,问题就出现了

例如:
GET/v1/content/merchants/{string}

url可以是
GET/v1/content/merchants/foo
GET/v1/content/merchants/bar

我想在后端存储上述格式,如
GET/v1/content/merchants/string
,因为我无法在后端
GET/v1/content/merchants/foo
GET/v1/content/merchants/bar>中存储每个传入请求

下面是代码

local function get_method(request)
  local method, path, query_fragment = request:match("^(.+) ([^%?]+)(%??.*) .+$")
  if method and path then
    return method .. "_" .. path
  else
    return nil
  end
end

local function extract_usage(request)
  local usage_t =  {}
  local ts_method = get_method(request)
  if ts_method then
    usage_t[ts_method] = set_or_inc(usage_t, ts_method, 1)
    return build_querystring(usage_t)
  else
    return nil
  end
end
我建议使用

local function get_method_updated(request)
  local method, path, query_fragment = request:match("^(.+) ([^%?]+)(%??.*) .+$")
  if method and path then
    if path:match('^/[^/]+/[^/]+/[%w_]+') ~= nil then
        return method .. " " .. path
    else
        return method .. "_" .. path
    end 
  else
    return nil
  end
end

print(get_method_updated("GET /v1/merchants something"))
print(get_method_updated("GET /v1/content/merchants/foo or"))
print(get_method_updated("GET /v1/content/merchants/bar some"))

输出:

GET_/v1/merchants
GET /v1/content/merchants/foo
GET /v1/content/merchants/bar
更新的
get\u方法将确保您不会获取带下划线的请求部分作为返回值。

我建议使用

local function get_method_updated(request)
  local method, path, query_fragment = request:match("^(.+) ([^%?]+)(%??.*) .+$")
  if method and path then
    if path:match('^/[^/]+/[^/]+/[%w_]+') ~= nil then
        return method .. " " .. path
    else
        return method .. "_" .. path
    end 
  else
    return nil
  end
end

print(get_method_updated("GET /v1/merchants something"))
print(get_method_updated("GET /v1/content/merchants/foo or"))
print(get_method_updated("GET /v1/content/merchants/bar some"))

输出:

GET_/v1/merchants
GET /v1/content/merchants/foo
GET /v1/content/merchants/bar

更新的
get\u方法将确保您不会获取带下划线的请求部分作为返回值。

什么是
{id}
?除了字符串末尾的
/
以外的任何字符或仅数字?{id}是数据类型=字符串。它可以是/v1/content/merchants/foo或/v1/content/merchants/bar。我理解你的困惑,我将编辑这篇文章。我仍然不明白问题出在哪里
GET/v1/merchants
^(+.+)([^%?]+)(??.*)。+$
模式不匹配。请发布可复制的代码和预期输出。请检查。如果我没弄错的话,
get\u method\u updated
将确保您不会获取带下划线的请求部分作为返回值。什么是
{id}
?除了字符串末尾的
/
以外的任何字符或仅数字?{id}是数据类型=字符串。它可以是/v1/content/merchants/foo或/v1/content/merchants/bar。我理解你的困惑,我将编辑这篇文章。我仍然不明白问题出在哪里
GET/v1/merchants
^(+.+)([^%?]+)(??.*)。+$
模式不匹配。请发布可复制的代码和预期输出。请检查。如果我没弄错的话,
get\u method\u updated
将确保您不会获取带下划线的请求部分作为返回值。