import re


def re_map(f, regexp, string):
  """Find regexp in string and map f on the matches.

  Replace every occurence of regexp in string by the application of f
  to the match.

  >>> re_map(lambda x: '<%s>' % x.capitalize(), 'a|e|i|o|u', 'This was a triumph')
  'Th<I>s w<A>s <A> tr<I><U>mph'
  """
  match = re.search(regexp, string)
  while match:
    prefix = string[:match.span()[0]]
    suffix = string[match.span()[1]:]
    string = '%s%s%s' % (prefix, f(match.group()), suffix)
    match = re.search(regexp, string)
  return string