3

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.

3
  • in your original string - you have id:0 and id=1. Does this mean that the number after id can be separated by either a : or a = ?? Commented Oct 31, 2022 at 7:51
  • it will be separated by : Commented Oct 31, 2022 at 8:02
  • I edited my answer to match only a : or = separator. If it's only a : then you also just use r'id:(\d+),[\w\s]+' + word Commented Oct 31, 2022 at 8:16

1 Answer 1

5

You can use the following regex to return the indices and key word matches.

import re

def find_id(string, word):
    pattern = r'id[:=](\d+),[\w\s]+' + word
    return list(map(int, re.findall(pattern, string)))

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"

print(find_id(string, 'fox'))

Output:

[3, 4]

I have returned ints but if this isn't necessary then you can return the indices as strings ['3', '4'] by replacing the return line with;

return re.findall(pattern, string)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.