-4

I have such a string below and I want to extract the url just after the imgurl:. How could you do in Python in handy way?

{ns:"images",k:"5049",mid:"551FC833EDC139718135AA91A46D6B09FE89E85C",surl:"http://www.thewritingnut.com/blog-challenge/az-day-25-yellow-symbolisms/",imgurl:"http://www.thewritingnut.com/wp-content/uploads/2011/04/yellow-rose-800.jpg",oh:"199",tft:"0",oi:"http://www.thewritingnut.com/wp-content/uploads/2011/04/yellow-rose-800.jpg"}
erogol
  • 13,156
  • 33
  • 101
  • 155
  • Where do get that structure from? Isn't that a dictionary? – Rohit Jain Sep 22 '13 at 11:38
  • say, what have you tried? – Herrington Darkholme Sep 22 '13 at 11:38
  • 1
    possible duplicate of [Converting a String to Dictionary?](http://stackoverflow.com/questions/988228/converting-a-string-to-dictionary) There is no need to use regex here. Just convert the data into a more manageable form. – Lix Sep 22 '13 at 11:39
  • 2
    One way is to use `demjson` a non-strict json parser - see http://stackoverflow.com/questions/17147900/how-to-convert-unicode-dict-to-dict/17148097#17148097 – Jon Clements Sep 22 '13 at 11:41

1 Answers1

0

Here it is, using a regular expression:

data = '{ns:"images",k:"5049",mid:"551FC833EDC139718135AA91A46D6B09FE89E85C",surl:"http://www.thewritingnut.com/blog-challenge/az-day-25-yellow-symbolisms/",imgurl:"http://www.thewritingnut.com/wp-content/uploads/2011/04/yellow-rose-800.jpg",oh:"199",tft:"0",oi:"http://www.thewritingnut.com/wp-content/uploads/2011/04/yellow-rose-800.jpg"}'

re.search('imgurl:"([^"]+)', data).group(1)
urish
  • 8,943
  • 8
  • 54
  • 75
  • Yes or `re.search('imgurl:"(.+?)"',data).group(1)` – pandita Sep 22 '13 at 11:47
  • 2
    This is crazy. If it's JSON or a dictionary, convert to dict and use appropriate appropriate search in dictionary -- like on key or using regex on the value. Rigging up a regular expression search on a dictionary-as-string just because OP asked for that is not a good answer, IMO. – hughdbrown Sep 22 '13 at 11:55