haregex re compile( '(ha){ }'mo haregex search('hahaha'mo group('hahahamo haregex search('ha'mo =none true here(ha){ matches 'hahahabut not 'hasince it doesn' match 'ha'search(returns none greedy and non-greedy matching since (ha){ , can match threefouror five instances of ha in the string 'hahahahaha'you may wonder why the match object' call to group(in the previous brace example returns 'hahahahahainstead of the shorter possibilities after all'hahahaand 'hahahahaare also valid matches of the regular expression (ha){ , python' regular expressions are greedy by defaultwhich means that in ambiguous situations they will match the longest string possible the nongreedy (also called lazyversion of the braceswhich matches the shortest string possiblehas the closing brace followed by question mark enter the following into the interactive shelland notice the difference between the greedy and non-greedy forms of the braces searching the same stringgreedyharegex re compile( '(ha){ , }'mo greedyharegex search('hahahahaha'mo group('hahahahahanongreedyharegex re compile( '(ha){ , }?'mo nongreedyharegex search('hahahahaha'mo group('hahahanote that the question mark can have two meanings in regular expressionsdeclaring non-greedy match or flagging an optional group these meanings are entirely unrelated the findall(method in addition to the search(methodregex objects also have findall(method while search(will return match object of the first matched text in the searched stringthe findall(method will return the strings of every pattern matching with regular expressions