有 2 种方法可以走,要么验证需要删除整条线,
要么剪掉这条线的违规部分。我不知道您使用的是哪种正则表达式引擎。为了获得更好的结果,至少需要负面的回溯和反向引用。
压缩验证:
/^(?=(Set-Cookie:\s*))(?:\1|.*?;\s*)(?:__[a-z]+|__utma_a2a|has_js|Drupal\.toolbar\.collapsed|MNO_abc_qrst_\d+)=[^;]*(?:;|$).*$/s
压缩的全局零件替换:
s/(?:(?<=^Set-Cookie:)|(?<=;))\s*(?:__[a-z]+|__utma_a2a|has_js|Drupal\.toolbar\.collapsed|MNO_abc_qrst_\d+)=[^;]*(?:;|$)//g
正则表达式的扩展视图(在 Perl 中):
my $str = 'Set-Cookie: MNO_abc_qrst_80=aaaaaaaaa2a5882045525d5a4a58455e445a4a423660;expires=Fri, 12-Aug-2011 17:23:23 GMT;path=/;httponly';
## Validate part of a cookie, remove line
if ( $str =~ /
^ (?= (Set-Cookie: \s*))
(?: \1
| .*? ; \s*
)
(?: __[a-z]+
| __utma_a2a
| has_js
| Drupal\.toolbar\.collapsed
| MNO_abc_qrst_\d+
)
=
[^;]*
(?: ; | $)
.* $
/sx )
{
print "Valid, remove line '$&'\n=============\n\n";
}
## Globally, replace many parts of the cookie
if ( $str =~ s/
(?: (?<= ^ Set-Cookie:)
| (?<= ;)
)
\s*
(?: __[a-z]+
| __utma_a2a
| has_js
| Drupal\.toolbar\.collapsed
| MNO_abc_qrst_\d+
)
=
[^;]*
(?: ; | $)
//xg )
{
print "removed parts of cookie\n";
print "new string = '$str'\n";
}
输出:
Valid, remove line 'Set-Cookie: MNO_abc_qrst_80=aaaaaaaaa2a5882045525d5a4a58455e
445a4a423660;expires=Fri, 12-Aug-2011 17:23:23 GMT;path=/;httponly'
=============
removed parts of cookie
new string = 'Set-Cookie:expires=Fri, 12-Aug-2011 17:23:23 GMT;path=/;httponly'