匹配 cookie 字符串中的所有内容,特定键后的值除外
Matching everything in a cookie string except the value after a specific key
Cookie 字符串:
ViewerUserId=134; ThisUserId=124; session=ntrhtrh5ttrg54htrth54654654trtrgtgtrgtr
我有Varnish
,它使用 PCRE 来缓存内容,我想使用正则表达式来匹配除 ThisUserId
之后的值以外的所有内容,在这种情况下,它应该是 124
。
我可以使用 ThisUserId=([0-9]+)\b
或 (?:^|;\s*)ThisUserId=(.*?)(?:;|,(?!\s))
提取 124
,但是,要在 Varnish
中设置变量,我需要匹配除目标之外的所有内容value 然后用空字符串替换匹配的字符串,有点像这样:
set req.http.ThisUserId = regsub(req.http.Cookie,"MATCH_EVEYRTHING_EXCEPT_THISUSERID","");
set req.http.ViewerUserId = regsub(req.http.Cookie,"MATCH_EVEYRTHING_EXCEPT_VIEWERUSERID","");
if(req.http.ViewerUserId == req.http.ThisUserId){
return(pass); // Don't cache if viewer is browsing his own pages
}
任何人都可以向我展示在特定字符串后匹配除未知值以外的任何内容的示例吗?
您可以使用反向引用来做到这一点。这意味着您 select 带有括号的正则表达式的某些部分,然后可以在替换字符串中引用它们。所以,例如:
set req.http.ThisUserId = regsub(req.http.Cookie,".*ThisUserId=(\d+).*","");
set req.http.ViewerUserId = regsub(req.http.Cookie,".*ViewUserId=(\d+).*","");
Cookie 字符串:
ViewerUserId=134; ThisUserId=124; session=ntrhtrh5ttrg54htrth54654654trtrgtgtrgtr
我有Varnish
,它使用 PCRE 来缓存内容,我想使用正则表达式来匹配除 ThisUserId
之后的值以外的所有内容,在这种情况下,它应该是 124
。
我可以使用 ThisUserId=([0-9]+)\b
或 (?:^|;\s*)ThisUserId=(.*?)(?:;|,(?!\s))
提取 124
,但是,要在 Varnish
中设置变量,我需要匹配除目标之外的所有内容value 然后用空字符串替换匹配的字符串,有点像这样:
set req.http.ThisUserId = regsub(req.http.Cookie,"MATCH_EVEYRTHING_EXCEPT_THISUSERID","");
set req.http.ViewerUserId = regsub(req.http.Cookie,"MATCH_EVEYRTHING_EXCEPT_VIEWERUSERID","");
if(req.http.ViewerUserId == req.http.ThisUserId){
return(pass); // Don't cache if viewer is browsing his own pages
}
任何人都可以向我展示在特定字符串后匹配除未知值以外的任何内容的示例吗?
您可以使用反向引用来做到这一点。这意味着您 select 带有括号的正则表达式的某些部分,然后可以在替换字符串中引用它们。所以,例如:
set req.http.ThisUserId = regsub(req.http.Cookie,".*ThisUserId=(\d+).*","");
set req.http.ViewerUserId = regsub(req.http.Cookie,".*ViewUserId=(\d+).*","");