Python Forum
Finding multiple strings between the two same strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding multiple strings between the two same strings
#1
I'm just starting to learn Python and have hit my first real road block. I have a need for a file renamer and figured it would be a good way to start learning.

I'm trying to extract all of the titles from the following string (Basically each title is between title":" and ","):

{"page":1,"total_results":6,"total_pages":1,"results":[{"vote_count":2312,"id":2789,"video":false,"vote_average":6.4,"title":"The Chronicles of Riddick","popularity":7.467,"poster_path":"\/hVAfGskQdktDwYTqrT7wY1f2M4y.jpg","original_language":"en","original_title":"The Chronicles of Riddick","genre_ids":[28,878],"backdrop_path":"\/AoLVfcYPziZZQbAFQuKoceHreXD.jpg","adult":false,"overview":"After years of outrunning ruthless bounty hunters, escaped convict Riddick suddenly finds himself caught between opposing forces in a fight for the future of the human race. Now, waging incredible battles on fantastic and deadly worlds, this lone, reluctant hero will emerge as humanity's champion - and the last hope for a universe on the edge of annihilation.","release_date":"2004-06-11"},{"vote_count":0,"id":374771,"video":false,"vote_average":0,"title":"The Chronicles of Riddick: Furia","popularity":9.942,"poster_path":"\/lV5qk31axl9mo0uq9L4Jkmuzrbq.jpg","original_language":"en","original_title":"The Chronicles of Riddick: Furia","genre_ids":[878,28],"backdrop_path":null,"adult":false,"overview":"When Riddick finally finds his home planet, he has no idea what's in store for him.","release_date":""},{"vote_count":188,"id":2791,"video":false,"vote_average":6.1,"title":"The Chronicles of Riddick: Dark Fury","popularity":2.059,"poster_path":"\/ewqSDOOyjyODmyuVuxHmKGYKZR9.jpg","original_language":"en","original_title":"The Chronicles of Riddick: Dark Fury","genre_ids":[28,16,878,53],"backdrop_path":"\/jrZbDux6c99QxpxHqnLS6W8Jffc.jpg","adult":false,"overview":"After their narrow escape at the end of \"Pitch Black,\" Riddick, Jack and the Imam find themselves at the mercy of a madwoman who intends to entomb Riddick forever as part of a twisted art exhibit. With little but a shiv and Riddick's innate viciousness to aid them, Riddick and his allies must find a way to escape from their captor and her band of mercenaries.","release_date":"2004-06-15"},{"vote_count":2620,"id":2787,"video":false,"vote_average":6.8,"title":"Pitch Black","popularity":8.883,"poster_path":"\/j4iBVA49KDBX4ypfVcZxl1y7CCT.jpg","original_language":"en","original_title":"Pitch Black","genre_ids":[53,878,28],"backdrop_path":"\/yc0cEEZ9OO6ydyYmCEfY90Q9jII.jpg","adult":false,"overview":"When their ship crash-lands on a remote planet, the marooned passengers soon learn that escaped convict Riddick isn't the only thing they have to fear. Deadly creatures lurk in the shadows, waiting to attack in the dark, and the planet is rapidly plunging into the utter blackness of a total eclipse. With the body count rising, the doomed survivors are forced to turn to Riddick with his eerie eyes to guide them through the darkness to safety. With time running out, there's only one rule: Stay in the light.","release_date":"2000-02-18"},{"vote_count":2845,"id":87421,"video":false,"vote_average":6.2,"title":"Riddick","popularity":15.891,"poster_path":"\/duoN6oNGp1emHkkhlTcY76Wz2ED.jpg","original_language":"en","original_title":"Riddick","genre_ids":[878,28,53],"backdrop_path":"\/mLbjD5xyrjLzVviSd8l37rxfWBo.jpg","adult":false,"overview":"Betrayed by his own kind and left for dead on a desolate planet, Riddick fights for survival against alien predators and becomes more powerful and dangerous than ever before. Soon bounty hunters from throughout the galaxy descend on Riddick only to find themselves pawns in his greater scheme for revenge. With his enemies right where he wants them, Riddick unleashes a vicious attack of vengeance before returning to his home planet of Furya to save it from destruction.","release_date":"2013-09-02"},{"vote_count":49,"id":211449,"video":false,"vote_average":6.7,"title":"Riddick: Blindsided","popularity":1.249,"poster_path":"\/AiWr89Y4g6gkJ3WhVv1TrcSTFMf.jpg","original_language":"en","original_title":"Riddick: Blindsided","genre_ids":[28,16,878],"backdrop_path":"\/7i9W0PzRGbxv8MJ6cYIIkAxyubE.jpg","adult":false,"overview":"This motion comic gaps the bridge between 2004's The Chronicles of Riddick, and 2013's Riddick. A new attempt on Lord Marshal Riddick's life is made on the Necromongers' ship. It's time for Riddick to end the years long meandering through space and go back to his roots.","release_date":"2013-07-26"}]}

I tried the following, but this seems to use the last "," of the string.

regex = '(?<="title":")(.*)(?=",")'
results = re.findall(regex,result)
print(results)
Reply
#2
import re

result = """{"page":1,"total_results":6,"total_pages":1,"results":[{"vote_count":2312,"id":2789,"video":false,"vote_average":6.4,"title":"The Chronicles of Riddick","popularity":7.467,"poster_path":"\/hVAfGskQdktDwYTqrT7wY1f2M4y.jpg","original_language":"en","original_title":"The Chronicles of Riddick","genre_ids":[28,878],"backdrop_path":"\/AoLVfcYPziZZQbAFQuKoceHreXD.jpg","adult":false,"overview":"After years of outrunning ruthless bounty hunters, escaped convict Riddick suddenly finds himself caught between opposing forces in a fight for the future of the human race. Now, waging incredible battles on fantastic and deadly worlds, this lone, reluctant hero will emerge as humanity's champion - and the last hope for a universe on the edge of annihilation.","release_date":"2004-06-11"},{"vote_count":0,"id":374771,"video":false,"vote_average":0,"title":"The Chronicles of Riddick: Furia","popularity":9.942,"poster_path":"\/lV5qk31axl9mo0uq9L4Jkmuzrbq.jpg","original_language":"en","original_title":"The Chronicles of Riddick: Furia","genre_ids":[878,28],"backdrop_path":null,"adult":false,"overview":"When Riddick finally finds his home planet, he has no idea what's in store for him.","release_date":""},{"vote_count":188,"id":2791,"video":false,"vote_average":6.1,"title":"The Chronicles of Riddick: Dark Fury","popularity":2.059,"poster_path":"\/ewqSDOOyjyODmyuVuxHmKGYKZR9.jpg","original_language":"en","original_title":"The Chronicles of Riddick: Dark Fury","genre_ids":[28,16,878,53],"backdrop_path":"\/jrZbDux6c99QxpxHqnLS6W8Jffc.jpg","adult":false,"overview":"After their narrow escape at the end of \"Pitch Black,\" Riddick, Jack and the Imam find themselves at the mercy of a madwoman who intends to entomb Riddick forever as part of a twisted art exhibit. With little but a shiv and Riddick's innate viciousness to aid them, Riddick and his allies must find a way to escape from their captor and her band of mercenaries.","release_date":"2004-06-15"},{"vote_count":2620,"id":2787,"video":false,"vote_average":6.8,"title":"Pitch Black","popularity":8.883,"poster_path":"\/j4iBVA49KDBX4ypfVcZxl1y7CCT.jpg","original_language":"en","original_title":"Pitch Black","genre_ids":[53,878,28],"backdrop_path":"\/yc0cEEZ9OO6ydyYmCEfY90Q9jII.jpg","adult":false,"overview":"When their ship crash-lands on a remote planet, the marooned passengers soon learn that escaped convict Riddick isn't the only thing they have to fear. Deadly creatures lurk in the shadows, waiting to attack in the dark, and the planet is rapidly plunging into the utter blackness of a total eclipse. With the body count rising, the doomed survivors are forced to turn to Riddick with his eerie eyes to guide them through the darkness to safety. With time running out, there's only one rule: Stay in the light.","release_date":"2000-02-18"},{"vote_count":2845,"id":87421,"video":false,"vote_average":6.2,"title":"Riddick","popularity":15.891,"poster_path":"\/duoN6oNGp1emHkkhlTcY76Wz2ED.jpg","original_language":"en","original_title":"Riddick","genre_ids":[878,28,53],"backdrop_path":"\/mLbjD5xyrjLzVviSd8l37rxfWBo.jpg","adult":false,"overview":"Betrayed by his own kind and left for dead on a desolate planet, Riddick fights for survival against alien predators and becomes more powerful and dangerous than ever before. Soon bounty hunters from throughout the galaxy descend on Riddick only to find themselves pawns in his greater scheme for revenge. With his enemies right where he wants them, Riddick unleashes a vicious attack of vengeance before returning to his home planet of Furya to save it from destruction.","release_date":"2013-09-02"},{"vote_count":49,"id":211449,"video":false,"vote_average":6.7,"title":"Riddick: Blindsided","popularity":1.249,"poster_path":"\/AiWr89Y4g6gkJ3WhVv1TrcSTFMf.jpg","original_language":"en","original_title":"Riddick: Blindsided","genre_ids":[28,16,878],"backdrop_path":"\/7i9W0PzRGbxv8MJ6cYIIkAxyubE.jpg","adult":false,"overview":"This motion comic gaps the bridge between 2004's The Chronicles of Riddick, and 2013's Riddick. A new attempt on Lord Marshal Riddick's life is made on the Necromongers' ship. It's time for Riddick to end the years long meandering through space and go back to his roots.","release_date":"2013-07-26"}]}"""


regex = '(?<="title":")(.*?)","'
results = re.findall(regex, result)
print(results)
Output:
['The Chronicles of Riddick', 'The Chronicles of Riddick: Furia', 'The Chronicles of Riddick: Dark Fury', 'Pitch Black', 'Riddick', 'Riddick: Blindsided']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print function output wrong with strings. mposwal 5 347 Feb-12-2021, 09:04 AM
Last Post: DPaul
Thumbs Down extracting data/strings from Word doc mikkelibsen 1 205 Feb-10-2021, 11:06 AM
Last Post: Larz60+
  How to compare strings that have spaces hobbyist 2 271 Jan-14-2021, 12:44 PM
Last Post: perfringo
  read logfile between two specific strings FelixReiter 6 357 Jan-04-2021, 02:26 PM
Last Post: FelixReiter
  extract particular strings into a variable lokesh 3 307 Dec-09-2020, 11:59 AM
Last Post: lokesh
  Strange syntax error with f-strings Askic 6 658 Oct-16-2020, 10:40 AM
Last Post: Askic
  Convert file of hex strings to binary file medatib531 4 627 Oct-09-2020, 05:42 PM
Last Post: DeaD_EyE
  input inbetween two strings? KEYS 3 455 Sep-23-2020, 07:30 AM
Last Post: snippsat
  Mutable Strings millpond 3 448 Aug-24-2020, 08:42 AM
Last Post: millpond
  printing strings with format leodavinci1990 1 488 Aug-21-2020, 02:00 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020