WR7500
WR7500

Reputation: 449

How to find a string after a specific set of text?

I'm trying to capture the 24-character string from behind the _id field in the source below:

[{"actors":"Natalie Portman, Hugo Weaving, Stephen Rea","year":2006,"description":"","title":"V for Vendetta","image":"http:\/\/content8.flixster.com\/movie\/11\/16\/67\/11166734_det.jpg","rating":3.65,"_id":"4eb04794f5f8077d1d000000","links":{"rottentomatoes":"http:\/\/www.rottentomatoes.com\/m\/v_for_vendetta\/","imdb":"http:\/\/www.imdb.com\/title\/tt0434409\/","shortUrl":"http:\/\/www.canistream.it\/search\/movie\/4eb04794f5f8077d1d000000\/v-for-vendetta"}},{"actors":"Guy Madison, Monica Randall, Mariano Vidal Molina","year":1966,"description":"","title":"I Cinque della vendetta (Five for Revenge)(The Five Giants from Texas)(No Drums No Trumpets)","image":"http:\/\/images.rottentomatoescdn.com\/images\/redesign\/poster_default.gif","rating":-0.05,"_id":"4e663229f5f8071702000002","links":{"imdb":"http:\/\/www.imdb.com\/title\/tt0060238\/","rottentomatoes":"http:\/\/www.rottentomatoes.com\/m\/i-cinque-della-vendetta-five-for-revengethe-five-giants-from-texasno-drums-no-trumpets\/","shortUrl":"http:\/\/www.canistream.it\/search\/movie\/4e663229f5f8071702000002\/i-cinque-della-vendetta-five-for-revenge-the-five-giants-from-texas-no-drums-no-trumpets-"}}]

I've tried using a lookbehind as below, but no luck.

^(?<=_id":")[a-z0-9]{24}

I'm using this as part of a Python script, if it makes a difference.

Upvotes: 0

Views: 61

Answers (3)

Heikki Toivonen
Heikki Toivonen

Reputation: 31130

Like the other two answers stated, if you have the original data structure use those. But if all of that fails, this could perhaps work:

pat = '_id":"'
i = s.find(pat)
if i >= 0:
    i += len(pat)
value = s[i:i+24]

Upvotes: 1

CT Zhu
CT Zhu

Reputation: 54330

This is a list, inside of which there is a dictionary, if it is called D

>>> D[0]['_id']
   '4eb04794f5f8077d1d000000'

Upvotes: 1

shaktimaan
shaktimaan

Reputation: 12092

If the above data is a json object stored in a variable, say data

data[0]['_id'] 

gives what you want.

If it is a string, load it as a json, using the python's json module and access the data as above, i.e.,

import json
data_j = json.loads(data)
data_j[0]['_id'] 

Upvotes: 1

Related Questions