regular expressions groups using parentheses around part of an expression creates group that contains the text that matches pattern you can use this to do more sophisticated substitutions here is an example that converts to lowercase every capital letter that is followed by lowercase letterdef modify(match)letters match group(return letters lower(re sub( '([ - ])[ - 'modify'peach apple apricot 'peach apple apricot the modify function ends up getting called three timesone for each time match occurs the re sub function automatically sends to the modify function match objectwhich we name match this object contains information about the matching text the object' group method returns the matching text itself if instead of match groupwe use match groupsthen we can further break down the match according the groups defined by the parentheses here is an example that matches capital letter followed by digit and converts each letter to lowercase and adds to each numberdef modify(match)letternumber match groups(return letter lower(str(int(number)+ re sub( '([ - ])(\ 'modify' ' the groups method returns the matching text as tuples for instancein the above program the tuples returned are shown belowfirst match(' '' 'second match(' '' 'third match(' '' 'note also that we can get at this information by passing arguments to match group for the first matchmatch group( is 'aand match group( is other functions sub -we have seen many examples of sub one thing we haven' mentioned is that there is an optional argument countwhich specifies how many matches (from the leftto make here is an examplere sub( ' ''''ababababa 'count= '* *bababa