code: def _compile_path_pattern(pattern):
      r
  
      def replace_variable(match):
        """Replaces a {variable} with a regex to match it by name.
  
        Changes the string corresponding to the variable name to the base32
        representation of the string, prepended by an underscore. This is
        necessary because we can have message variable names in URL patterns
        (e.g. via {x.y}) but the character '.' can't be in a regex group name.
  
        Args:
          match: A regex match object, the matching regex group as sent by
            re.sub().
  
        Returns:
          A string regex to match the variable by name, if the full pattern was
          matched.
        """
        if match.lastindex > 1:
          var_name = ApiConfigManager._to_safe_path_param_name(match.group(2))
          return '%s(?P<%s>%s)' % (match.group(1), var_name,
                                   _PATH_VALUE_PATTERN)
        return match.group(0)
  
      pattern = re.sub('(/|^){(%s)}(?=/|$|:)' % _PATH_VARIABLE_PATTERN,
                       replace_variable, pattern)
      return re.compile(pattern + '/?$')
docs: r"""Generates a compiled regex pattern for a path pattern.
  
      e.g. '/MyApi/v1/notes/{id}'
      returns re.compile(r'/MyApi/v1/notes/(?P<id>[^/?#\[\]{}]*)')
  
      Args:
        pattern: A string, the parameterized path pattern to be checked.
  
      Returns:
        A compiled regex object to match this path pattern.