Hi all,

I have been having a problem for a little while with regards to strings.

The problem is that i want to find two different words in a string then return the words between those 2 words.

ie: If i had the string "The quick brown fox jumps over the lazy dog." and I want to find the sub strings "The" and "fox" and return the sub string between them.

ie: the answer would be "quick brown"

i know it is confusing but how would i go about it

Thx,

daiosmith

I'm sorry for the stupid above post which is only advertising a different site.
Anyways, why not try:

sentence = "The quick brown fox jumps over the lazy dog."
search1 = "the" # start word
search2 = "fox" # end word
index1 = sentence.lower().find( search1 ) # search through as lowercase
index2 = sentence.lower().find( search2 )
words = sentence[ index1 + len(search1) : index2 ].split(" ") # split into list of words
for a in range( len(words) ): # remove blank spaces
    try:
        if words[ a ] == "":
            del words[ a ]
    except:
        pass
print words # output

Result: ['quick', 'brown'] It looks fairly long for such a simple function, but all that the for loops does is remove any blank spaces. The result is the list words , which contains the in-between words.
Hope that helps!

The following splits the sentence up into words first, looks for the index of word 1, then looks for the index of word 2 starting after index 1. Punctuation and whitespace is stripped from each word.

import string

def words_between(s, first, second):
    # return the words between first and second words
    # strip punctuation and whitespace
    words = [word.lower().strip(string.punctuation+string.whitespace) \
             for word in s.split()]
    # catch error if word not in sentence
    try:
        idx1 = words.index(first.lower())
        # start search for second word after idx1
        idx2 = words.index(second.lower(),idx1+1)
        return words[idx1+1:idx2]
    except ValueError, e:
        return "ValueError: %s" % e

sentence = "The quick brown fox jumps over the lazy dog."
print words_between(sentence, "fox", "the")
print words_between(sentence, "the", "the")
print words_between(sentence, "the", "cat")

Output:

>>> ['jumps', 'over']
['quick', 'brown', 'fox', 'jumps', 'over']
ValueError: list.index(x): x not in list
>>>

Here is my solution. Same notation as shadwickman.

def function(sentence, search1,search2):
    list=sentence.lower().split()
    return ' '.join(list[list.index(search1)+1:list.index(search2)])

Output:

>>> function("The quick brown fox jumps over the lazy dog.","the","fox")
'quick brown'

I think solsteel's idea about splitting the sentence into a list first is much better (why didn't I think of that?), and he implemented an error catching system. I hope all the responses answer your question Daiosmith!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.