Python's method of getting intermediate strings based on beginning and end strings

  • 2020-04-02 14:42:27
  • OfStack

This example shows how python gets intermediate strings based on beginning and end strings. Share with you for your reference. Specific analysis is as follows:

Given a string, specify the beginning and end of the string, return the middle string, such as:
Content: < Div class = "a" > jb51.net < / div >
StartStr: < Div class = "a" >
EndStr: < / div >
Return result: jb51.net


def GetMiddleStr(content,startStr,endStr):
  startIndex = content.index(startStr)
  if startIndex>=0:
    startIndex += len(startStr)
  endIndex = content.index(endStr)
  return content[startIndex:endIndex]
if __name__=='__main__':
  print(GetMiddleStr('<div class="a">jb51.net</div>','<div class="a">','</div>'))

I hope this article has helped you with your Python programming.


Related articles: