importrewikiText="""==Mainsection1==Some text here===Subsection1.1===Other text here==Mainsection2==Text goes here===Subsecttion2.1===Other text goes here. """outputArray=re.findall('(?<!=)==([^=]+)==(?!=)([\s\S]*?(?=$|(?<!=)==[^=]+==(?!=)))',wikiText)printoutputArrayoutputArray=re.findall('(?<!=)==([^=]*)==(?!=)([^=]*)',wikiText,re.MULTILINE)printoutputArray