【问题标题】:string.gsub isn't finding my patternstring.gsub 没有找到我的模式
【发布时间】:2016-02-23 16:23:43
【问题描述】:

我知道一个非常通用的标题,我的问题是我在 lua 中编码并且我正在尝试做一个 string.gsub 但它似乎没有找到我的模式。在下面找到所有详细信息。任何帮助表示赞赏。

Msg 变量包含:

{
    "started_at":1456170948017,
    "response":{
        "status":0,
        "size":"0",
        "headers":{
            "connection":"keep-alive"
        }
    },
    "authenticated_entity":{
        "consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
        "id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
    },
    "request":{
        "method":"POST",
        "uri":"\/SecureApi\/Logon",
        "size":"565",
        "request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
        "querystring":{

        },
        "headers":{
            "x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
            "accept-language":"en-US,en;q=0.8",
            "content-type":"application\/json",
            "connection":"keep-alive",
            "content-length":"78",
            "x-consumer-username":"kylec",
            "cache-control":"no-cache",
            "host":"192.168.10.122:8000",
            "cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
            "accept-encoding":"gzip, deflate",
            "origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
            "accept":"*\/*",
            "apikey":"test",
            "postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
            "user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
        }
    },
    "client_ip":"192.168.10.143",
    "api":{
        "upstream_url":"https:\/\/stage.site.com",
        "request_path":"\/SecureApi",
        "id":"90ba1710-1e50-4070-95d0-deb76d33e312",
        "created_at":1454625397000,
        "name":"secure_account_stage",
        "request_host":"stage.site.com"
    },
    "latencies":{
        "request":0,
        "kong":0,
        "proxy":-1
    }
}

代码:

string.gsub(msg, '"querystring":{}', '"querystring":{'..BuildURLParameters()..'}')
string.gsub(msg, '},"headers":{','},"body":{'..BuildBodyParameters()..'},"headers":{')

预期结果:

{
    "started_at":1456170948017,
    "response":{
        "status":0,
        "size":"0",
        "headers":{
            "connection":"keep-alive"
        }
    },
    "authenticated_entity":{
        "consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
        "id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
    },
    "request":{
        "method":"POST",
        "uri":"\/SecureApi\/Logon",
        "size":"565",
        "request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
        "querystring":{ somejsoninhere },
        "body":{ somejsoninhere },
        "headers":{
            "x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
            "accept-language":"en-US,en;q=0.8",
            "content-type":"application\/json",
            "connection":"keep-alive",
            "content-length":"78",
            "x-consumer-username":"kylec",
            "cache-control":"no-cache",
            "host":"192.168.10.122:8000",
            "cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
            "accept-encoding":"gzip, deflate",
            "origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
            "accept":"*\/*",
            "apikey":"test",
            "postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
            "user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
        }
    },
    "client_ip":"192.168.10.143",
    "api":{
        "upstream_url":"https:\/\/stage.site.com",
        "request_path":"\/SecureApi",
        "id":"90ba1710-1e50-4070-95d0-deb76d33e312",
        "created_at":1454625397000,
        "name":"secure_account_stage",
        "request_host":"stage.site.com"
    },
    "latencies":{
        "request":0,
        "kong":0,
        "proxy":-1
    }
}

结果:

{
    "started_at":1456170948017,
    "response":{
        "status":0,
        "size":"0",
        "headers":{
            "connection":"keep-alive"
        }
    },
    "authenticated_entity":{
        "consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
        "id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
    },
    "request":{
        "method":"POST",
        "uri":"\/SecureApi\/Logon",
        "size":"565",
        "request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
        "querystring":{

        },
        "headers":{
            "x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
            "accept-language":"en-US,en;q=0.8",
            "content-type":"application\/json",
            "connection":"keep-alive",
            "content-length":"78",
            "x-consumer-username":"kylec",
            "cache-control":"no-cache",
            "host":"192.168.10.122:8000",
            "cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
            "accept-encoding":"gzip, deflate",
            "origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
            "accept":"*\/*",
            "apikey":"test",
            "postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
            "user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
        }
    },
    "client_ip":"192.168.10.143",
    "api":{
        "upstream_url":"https:\/\/stage.site.com",
        "request_path":"\/SecureApi",
        "id":"90ba1710-1e50-4070-95d0-deb76d33e312",
        "created_at":1454625397000,
        "name":"secure_account_stage",
        "request_host":"stage.site.com"
    },
    "latencies":{
        "request":0,
        "kong":0,
        "proxy":-1
    }
}

【问题讨论】:

  • 首先:最好只是JSON解码,添加数据并再次JSON编码。其次: string.gsub 不会像您想象的那样忽略空格(如换行符)。例如'"querystring":{}' 应该是 '"querystring":{%s*}' 以使其也计算换行符。
  • @EinsteinK 好的,我明白了。尽管如果数据不是 JSON 格式,我必须查看数据的结构(表格等)。天真地,这些数据来自 nginx。

标签: lua gsub


【解决方案1】:

正如 @EinsteinK 指出的那样,您正在查询一个字符串文字,它不会考虑空格。

使其工作的最简单方法正是他所建议的:使用[["querystring":{%s*}]]

为了省去多余的“查询字符串”,你可以这样写:

string.gsub(msg, '("querystring"):{%s*}', '%1:{'..BuildURLParameters()..'}')

括号将匹配该部分表达式的任何内容放入capture%n 插入第 n 个捕获。

你可以使用捕获来缩进它,但如果漂亮的输出不是问题,你不应该为此烦恼。

Repl.it sample

输出:

{
    "started_at":1456170948017,
    "response":{
        "status":0,
        "size":"0",
        "headers":{
            "connection":"keep-alive"
        }
    },
    "authenticated_entity":{
        "consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
        "id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
    },
    "request":{
        "method":"POST",
        "uri":"\/SecureApi\/Logon",
        "size":"565",
        "request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
        "querystring":{somejson},
        "headers":{
            "x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
            "accept-language":"en-US,en;q=0.8",
            "content-type":"application\/json",
            "connection":"keep-alive",
            "content-length":"78",
            "x-consumer-username":"kylec",
            "cache-control":"no-cache",
            "host":"192.168.10.122:8000",
            "cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
            "accept-encoding":"gzip, deflate",
            "origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
            "accept":"*\/*",
            "apikey":"test",
            "postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
            "user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
        }
    },
    "client_ip":"192.168.10.143",
    "api":{
        "upstream_url":"https:\/\/stage.site.com",
        "request_path":"\/SecureApi",
        "id":"90ba1710-1e50-4070-95d0-deb76d33e312",
        "created_at":1454625397000,
        "name":"secure_account_stage",
        "request_host":"stage.site.com"
    },
    "latencies":{
        "request":0,
        "kong":0,
        "proxy":-1
    }
}   1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-06
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2021-10-18
    相关资源
    最近更新 更多