0

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]
Rakesh
82.9k17 gold badges86 silver badges122 bronze badges
asked Sep 28, 2020 at 10:20

2 Answers 2

5

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
answered Sep 28, 2020 at 10:25
Sign up to request clarification or add additional context in comments.

Comments

2

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'])
answered Sep 28, 2020 at 10:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.