#!/usr/bin/python3 # ==================================================================== # search for a sub-string # ==================================================================== import re REGX = re.compile(r'<.*?>') # -------------------------------------------------------------------- # ---- search a string for a sub-string # -------------------------------------------------------------------- def search_for_substr(regx,srcstr): print() print(f"searching '{srcstr}'") res = re.search(regx,srcstr) # ---- end of search? if not res: return (False,0,0,None) # ---- get search results start = res.start() end = res.end() substr = srcstr[start:end] return (True,start,end,substr) # -------------------------------------------------------------------- # ---- main # -------------------------------------------------------------------- if __name__ == '__main__': templates = [ 'the <adjective> <noun> <adverb> <verb> to the <location>.', '<noun> and <noun> did what?', 'professor <noun> in the <location> with the candlestick.', '<noun> ate a <noun> pudding while sitting on a <noun> in the <location>.', '<action> speak louder than words!' ] tmpl = templates[0] # test olny one template while True: tf,start,end,substr = search_for_substr(REGX,tmpl) if tf is False: break print(f'found {substr}') tmpl = tmpl[end:] print() print('end of string')