import re


def match_numbered_scope(specop, search_string,
                         return_group=True,
                         numbered=True):
  """
  Find a numbered scope matching a specop from REGISTERED_SPECOPS,
  and return it if found.
    Example: 'conv2d' will match '...conv2d_345/...' and return 'conv2d_345'.

  Args:
    specop: the specop to match.
    search_string: the op name to search.
    return group: if True, returns the last matching group, otherwise return
        the match object.
    numbered: only match exact numbering  -
        i.e. conv2d_1 will only match conv2d_1 and not conv2d etc.

  """
  if numbered:
    expr = '^(.*/)*({0})/|(^(.*/)*({0}_[0-9]+))/'.format(specop)
  else:
    expr = '^(.*/)*({0})/'.format(specop)

  match = re.search(expr, search_string)
  if match is not None:
    if not return_group:
      return match
    for grp in reversed(match.groups()):
      if grp is not None:
        return grp
  return match