user3552978
user3552978

Reputation:

Python Regex Match Integer After String

I need a regex in python to match and return the integer after the string "id": in a text file.

The text file contains the following:

{"page":1,"results": [{"adult":false,"backdrop_path":"/ba4CpvnaxvAgff2jHiaqJrVpZJ5.jpg","id":807,"original_title":"Se7en","release_date":"1995-09-22","p

I need to get the 807 after the "id", using a regular expression.

Upvotes: 1

Views: 2588

Answers (2)

zx81
zx81

Reputation: 41838

Is this what you mean?

#!/usr/bin/env python
import re

subject = '{"page":1,"results": [{"adult":false,"backdrop_path":"/ba4CpvnaxvAgff2jHiaqJrVpZJ5.jpg","id":807,"original_title":"Se7en","release_date":"1995-09-22","p'

match = re.search('"id":([^,]+)', subject)
if match:
    result = match.group(1)
else:
    result = "no result"
print result    

The Output: 807

Edit:

In response to your comment, adding one simple way to ignore the first match. If you use this, remember to add something like "id":809,"etc to your subject so that we can ignore 807 and find 809.

n=1
for match in re.finditer('"id":([^,]+)', subject):
    if n==1:
        print "ignoring the first match"
    else:
        print match.group(1)
    n+=1

Upvotes: 3

Sean Vieira
Sean Vieira

Reputation: 159875

Assuming that there is more to the file than that:

import json

with open('/path/to/file.txt') as f:
    data = json.loads(f.read())
    print(data['results'][0]['id'])

If the file is not valid JSON, then you can get the value of id with:

from re import compile, IGNORECASE

r = compile(r'"id"\s*:\s*(\d+)', IGNORECASE)

with open('/path/to/file.txt') as f:
    for match in r.findall(f.read()):
        print(match(1))

Upvotes: 2

Related Questions