Reputation: 542
I want to get the text in quotations after the occurrence of the pattern "name".
A sample string is:
info = "name: joe", "name: jerry", "name: kate"
Here is what I'm doing:
import re
string = 'info = "name: joe", "name: jerry", "name: kate"'
array = re.findall(r'"(.*?)"', string)
for x in array:
x = x.replace(" ","") #remove spaces because there might be space before colon occasionally
print(x.split("name:")[1])
The output prints:
joe
jerry
kate
I am just wondering if there is any easier way to do this and return the same output than what I did.
Upvotes: 0
Views: 108
Reputation: 18306
You can embed the name:
pattern in the regex, account for the occasional space before the colon via \s*
and also match one or more spaces after colon and the actual name (where you are splitting from):
re.findall(r'"name\s*:\s+(.*?)"', string)
# ['joe', 'jerry', 'kate']
Upvotes: 1
Reputation: 13387
Try:
res=list(re.findall(r'\"name: ([^\"]+)"', string))
print(res)
2 important things - you extract only a group, hence the square brackets, and you define a name as set of one or more characters, that aren't double quotes "
, since regex is on default greedy (i.e. matches the longest match only).
Outputs:
['joe', 'jerry', 'kate']
Upvotes: 1