How to extract a string between 2 other strings in python?

python, string

Solution

The solution is to use a regexp:

import re
r = re.compile('Master(.*?)thon')
m = r.search(str1)
if m:
    lyrics = m.group(1)

Problem

Like if I have a string like `str1 = "IWantToMasterPython"` If I want to extract `"Py"` from the above string. I write: ``` extractedString = foo("Master","thon") ``` I want to do all this because i am trying to extract lyrics from an html page. The lyrics are written like `<div class = "lyricbox"> ....lyrics goes here....</div>`. Any suggestions on how can I implement.

Original source