1

I am writing a function that is to return the words of a string between the firstWord and secondWord that are given by the user.

def returnWordsBetween(firstWord, secondWord, inputString):
    start = inputString.find(firstWord)
    end = inputString.find(secondWord)
    inputString =(start, end) 
    print inputString 

    firstWord = '<data>'
    secondWord = '</data>'
    inputString = 'fooz bizz <data> 31.25 funnel </data> fuzz'

Does this look to work? I am not getting anything to print out and no error codes, not sure what is up

1
  • 1
    indent last 3 line outwards(<--).. Commented Jul 30, 2015 at 5:46

4 Answers 4

3

You may use regex.

print re.search(firstWord+r'(.*?)'+secondWord, inputString).group(1)
Sign up to request clarification or add additional context in comments.

Comments

3

Use:

inputString[start + len(firstWord):end]

For example:

firstWord = '<data>'
secondWord = '</data>'
inputString = 'fooz bizz <data> 31.25 funnel </data> fuzz'
start = inputString.find(firstWord)
end = inputString.find(secondWord)
print inputString[start + len(firstWord):end]

Result:

 31.25 funnel

Comments

1

You can use slicing concept of python here. And can be solved in one line

inputString[ ( inputString.find(firstWord) + len(firstWord) ) : inputString.find(secondWord)]

Comments

0

You can use substring package by "pip install substring". This package is multi-operational and would be able to solve your problem.

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.