0

How can I extract text from a string when begining and ending strings are given?

s = "Waiting is a 2015 Indian comedy-drama film directed by Anu Menon, released in India on 27 May 2016. Produced by Priti Gupta of Ishka Films and Manish Mundra of Drishyam Films, the film was co-written by Menon and James Ruzicka"

begining = "Produced"
ending = "Films" 

Output should be:

Produced by Priti Gupta of Ishka Films and Manish Mundra of Drishyam Films

2
  • 1
    It's a bad idea to name your string str, as this shadows the built in str() method. I know what you posted is just an example, I thought I'd point this out just in case you didn't know :-) Commented May 27, 2017 at 13:34
  • Sorry I was in a hurry to ask the question out. Commented May 27, 2017 at 13:46

2 Answers 2

4

The solution using str.find and str.rfind functions with an additional check (assuring that both words are found):

s = "Waiting is a 2015 Indian comedy-drama film directed by Anu Menon, released in India on 27 May 2016. Produced by Priti Gupta of Ishka Films and Manish Mundra of Drishyam Films, the film was co-written by Menon and James Ruzicka"
begining_str = "Produced"
ending_str = "Films"

b_pos = s.find(begining_str)
e_pos = s.rfind(ending_str)
result = s[b_pos:e_pos + len(ending_str)] if b_pos != -1 and e_pos != -1 else ''

print(result)

The output:

Produced by Priti Gupta of Ishka Films and Manish Mundra of Drishyam Films
Sign up to request clarification or add additional context in comments.

4 Comments

I came across a problem. What if I have repeated words?
@BharathShetty, then, you should define the rules of the search.
link. This is the problem what I ran into.
@BharathShetty, you can initiate a new question with new issue
1

You can make use of the index method of strings in python and use slicing.

For your particular example something like this should work:

s[s.index(b):s.index(e)+len(e)]

What the code above does is the following: s.index(b) finds the first occurrence from the left of the string in b. Similar s.index(e). As you want to include the end word as well, we simply add the number of chars in the end string to the last index. s[x:y] gives you a "slice" of a string from x to y.

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.