1

I'm trying to extract properly some cookies from a web request. Basically i have this string:

 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"""

I want to remove all "Expires=Sun, 14-Jul-13 20:37:22 GMT," entries within this string. so this string become this:

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;"""

I was thinking of using Re for this :

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

Unfortunatly, i only get one result instead of all of the actual cookies

Any help or suggestion will be greatly appreciated.

Thanks !

1
  • Nice post. I like that you showed example input and the expected output. Commented Jul 14, 2012 at 21:15

3 Answers 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; '
Sign up to request clarification or add additional context in comments.

Comments

1

What about 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.

Comments

0

Look at the re.finditer function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.