I'm trying to return or print a specific substring that occurs before a search keyword in a very lengthy string using python.
For example if my string is as below
string = "id:0, an apple a day keeps the doctor away, id=1, oranges are orange not red, id=3, fox jumps over the dog, id=4, fox ate grapes"
If the search keyword is apple then 0 (which is the id) should be printed
if the search keyword is orange that 1 (which are the ids) should be printed
if the search keyword is fox then 3 and 4 (which is the id) should be printed
I'm expecting the id of the keyword as given in the example. In my case all the searchable keywords are associated with an id as in the example string.
id:0andid=1. Does this mean that the number after id can be separated by either a:or a=??::or=separator. If it's only a:then you also just user'id:(\d+),[\w\s]+' + word