1

我正在尝试从 Web 请求中正确提取一些 cookie。基本上我有这个字符串:

 str="""Cole_gal_langid=0; Expires=Sun, 14-Jul-13 20:37:22 GMT, Cole_gal_styleid=4; Expires=Sun, 14-Jul-13 20:37:22 GMT, Cole_gal_viewid=test; Expires=Sun, 14-Jul-13 20:37:22 GMT, Cole_gal_appid=gal; Expires=Sun, 14-Jul-13 20:37:22 GMT, Cole_gal_navk=common.invalidBookmark; Expires=Sun, 14-Jul-13 20:37:22 GMT, Cole_gal_trans=InvalidBookmark; Expires=Sun, 14-Jul-13 20:37:22 GMT"""

我想删除此字符串中的所有“Expires=Sun,14-Jul-13 20:37:22 GMT”条目。所以这个字符串变成了这样:

str="""Cole_gal_langid=0; Cole_gal_styleid=4; Cole_gal_viewid=test; Cole_gal_appid=gal; Cole_gal_navk=common.invalidBookmark; Cole_gal_trans=InvalidBookmark;"""

我正在考虑为此使用 Re:

import re

str="""Cole_gal_langid=0; Expires=Sun, 14-Jul-13 20:37:22 GMT, Cole_gal_styleid=4; Expires=Sun, 14-Jul-13 20:37:22 GMT, Cole_gal_viewid=test; Expires=Sun, 14-Jul-13 20:37:22 GMT, Cole_gal_appid=gal; Expires=Sun, 14-Jul-13 20:37:22 GMT, Cole_gal_navk=common.invalidBookmark; Expires=Sun, 14-Jul-13 20:37:22 GMT, Cole_gal_trans=InvalidBookmark; Expires=Sun, 14-Jul-13 20:37:22 GMT"""

a = re.search('(Cole_gal_*.\=*)[^;]*', str)
if a:
   quote = "Regex found this: "+a.group(0)+"\r\n"
   print quote

不幸的是,我只得到一个结果而不是所有实际的 cookie

任何帮助或建议将不胜感激。

谢谢 !

4

3 回答 3

2

Removes multiple occurrences of a pattern a job for re.sub:

>>> re.sub(r'Expires=.*?GMT([,;]|$)', '', s)
'Cole_gal_langid=0;  Cole_gal_styleid=4;  Cole_gal_viewid=test;  Cole_gal_appid=gal;  Cole_gal_navk=common.invalidBookmark;  Cole_gal_trans=InvalidBookmark; '
于 2012-07-14T21:12:50.623 回答
1

怎么样findall

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

于 2012-07-14T20:58:16.063 回答
0

re.finditer功能。

于 2012-07-14T20:57:44.173 回答