Extracting text after string

69 views Asked by At

I want to extract the string after "name=" from the following text. I have written the following regular expression but it isn't really working. The desired output is [Taal, Muntinlupa city]

    text = [ "id='00e5885868b4d7ed', url='https://api.twitter.com/1.1/geo/id/00e5885868b4d7ed.json', place_type='city', name='Taal', full_name='Taal, Calabarzon', country_code='PH', country='Republic of the Philippines'",
     "id='00c699d656122ebe', url='https://api.twitter.com/1.1/geo/id/00c699d656122ebe.json', place_type='city', name='Muntinlupa City', full_name='Muntinlupa City, National Capital Region', country_code='PH', country='Republic of the Philippines']

    matched_vals = [re.findall(r'(?<=name\=).*(?=\s)',tweet) for tweet in text]
2

There are 2 answers

0
Rakesh On BEST ANSWER

Use pattern r"name='(.+?)'"

Ex:

import re

text = [ "id='00e5885868b4d7ed', url='https://api.twitter.com/1.1/geo/id/00e5885868b4d7ed.json', place_type='city', name='Taal', full_name='Taal, Calabarzon', country_code='PH', country='Republic of the Philippines'",
 "id='00c699d656122ebe', url='https://api.twitter.com/1.1/geo/id/00c699d656122ebe.json', place_type='city', name='Muntinlupa City', full_name='Muntinlupa City, National Capital Region', country_code='PH', country='Republic of the Philippines'"
]

for i in text:
    print(re.search(r"name='(.+?)'", i).group(1))

Output:

Taal
Muntinlupa City
0
Mogi On

Create a dictionary out of the string, and that take the value of the key 'name':

dicts = []
for dic in text:
    dicts.append(ast.literal_eval(dic))

and then you can you these name (and other data very efficient):

for d in dicts:
    print(d['name'])