2016-02-23 204 views
1

我知道一個非常通用的標題,我的問題是我在lua中編碼,而我試圖做一個string.gsub,但它似乎沒有找到我的模式。下面找到所有的細節。任何幫助表示讚賞。string.gsub找不到我的模式

消息變量包含:

{ 
    "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 
    } 
} 
+1

首先:這將是最好只JSON對其進行解碼,添加數據和JSON重新編碼。其次:string.gsub不會忽略空格(就像換行符),因爲您似乎認爲這是空白。例如''''''querystring「:{}''''應該是'''''querystring」:{%s *}'''''''''''''''。 – EinsteinK

+0

@EinsteinK好吧,我明白了。儘管如果數據不在JSON中,我不得不考慮數據的結構(表格等)。天真地,這些數據來自nginx。 – Kyle

回答

0

由於@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