def match(uri, processor=nil)
      uri = Addressable::URI.parse(uri)
      mapping = {}

      # First, we need to process the pattern, and extract the values.
      expansions, expansion_regexp =
        parse_template_pattern(pattern, processor)

      return nil unless uri.to_str.match(expansion_regexp)
      unparsed_values = uri.to_str.scan(expansion_regexp).flatten

      if uri.to_str == pattern
        return Addressable::Template::MatchData.new(uri, self, mapping)
      elsif expansions.size > 0
        index = 0
        expansions.each do |expansion|
          _, operator, varlist = *expansion.match(EXPRESSION)
          varlist.split(',').each do |varspec|
            _, name, modifier = *varspec.match(VARSPEC)
            mapping[name] ||= nil
            case operator
            when nil, '+', '#', '/', '.'
              unparsed_value = unparsed_values[index]
              name = varspec[VARSPEC, 1]
              value = unparsed_value
              value = value.split(JOINERS[operator]) if value && modifier == '*'
            when ';', '?', '&'
              if modifier == '*'
                if unparsed_values[index]
                  value = unparsed_values[index].split(JOINERS[operator])
                  value = value.inject({}) do |acc, v|
                    key, val = v.split('=')
                    val = "" if val.nil?
                    acc[key] = val
                    acc
                  end
                end
              else
                if (unparsed_values[index])
                  name, value = unparsed_values[index].split('=')
                  value = "" if value.nil?
                end
              end
            end
            if processor != nil && processor.respond_to?(:restore)
              value = processor.restore(name, value)
            end
            if processor == nil
              if value.is_a?(Hash)
                value = value.inject({}){|acc, (k, v)|
                  acc[Addressable::URI.unencode_component(k)] =
                    Addressable::URI.unencode_component(v)
                  acc
                }
              elsif value.is_a?(Array)
                value = value.map{|v| Addressable::URI.unencode_component(v) }
              else
                value = Addressable::URI.unencode_component(value)
              end
            end
            if !mapping.has_key?(name) || mapping[name].nil?
              # Doesn't exist, set to value (even if value is nil)
              mapping[name] = value
            end
            index = index + 1
          end
        end
        return Addressable::Template::MatchData.new(uri, self, mapping)
      else
        return nil
      end
    end
def match(uri, processor=nil)
      uri = Addressable::URI.parse(uri)
      mapping = {}

      # First, we need to process the pattern, and extract the values.
      expansions, expansion_regexp =
        parse_template_pattern(pattern, processor)

      return nil unless uri.to_str.match(expansion_regexp)
      unparsed_values = uri.to_str.scan(expansion_regexp).flatten

      if uri.to_str == pattern
        return Addressable::Template::MatchData.new(uri, self, mapping)
      elsif expansions.size > 0
        index = 0
        expansions.each do |expansion|
          _, operator, varlist = *expansion.match(EXPRESSION)
          varlist.split(',').each do |varspec|
            _, name, modifier = *varspec.match(VARSPEC)
            mapping[name] ||= nil
            case operator
            when nil, '+', '#', '/', '.'
              unparsed_value = unparsed_values[index]
              name = varspec[VARSPEC, 1]
              value = unparsed_value
              value = value.split(JOINERS[operator]) if value && modifier == '*'
            when ';', '?', '&'
              if modifier == '*'
                if unparsed_values[index]
                  value = unparsed_values[index].split(JOINERS[operator])
                  value = value.inject({}) do |acc, v|
                    key, val = v.split('=')
                    val = "" if val.nil?
                    acc[key] = val
                    acc
                  end
                end
              else
                if (unparsed_values[index])
                  name, value = unparsed_values[index].split('=')
                  value = "" if value.nil?
                end
              end
            end
            if processor != nil && processor.respond_to?(:restore)
              value = processor.restore(name, value)
            end
            if processor == nil
              if value.is_a?(Hash)
                value = value.inject({}){|acc, (k, v)|
                  acc[Addressable::URI.unencode_component(k)] =
                    Addressable::URI.unencode_component(v)
                  acc
                }
              elsif value.is_a?(Array)
                value = value.map{|v| Addressable::URI.unencode_component(v) }
              else
                value = Addressable::URI.unencode_component(value)
              end
            end
            if !mapping.has_key?(name) || mapping[name].nil?
              # Doesn't exist, set to value (even if value is nil)
              mapping[name] = value
            end
            index = index + 1
          end
        end
        return Addressable::Template::MatchData.new(uri, self, mapping)
      else
        return nil
      end
    end
Extracts match data from the URI using a URI Template pattern.

 @param [Addressable::URI, #to_str] uri
   The URI to extract from.

 @param [#restore, #match] processor
   A template processor object may optionally be supplied.

   The object should respond to either the <tt>restore</tt> or
   <tt>match</tt> messages or both. The <tt>restore</tt> method should
   take two parameters: `[String] name` and `[String] value`.
   The <tt>restore</tt> method should reverse any transformations that
   have been performed on the value to ensure a valid URI.
   The <tt>match</tt> method should take a single
   parameter: `[String] name`. The <tt>match</tt> method should return
   a <tt>String</tt> containing a regular expression capture group for
   matching on that particular variable. The default value is `".*?"`.
   The <tt>match</tt> method has no effect on multivariate operator
   expansions.

 @return [Hash, NilClass]
   The <tt>Hash</tt> mapping that was extracted from the URI, or
   <tt>nil</tt> if the URI didn't match the template.

 @example
   class ExampleProcessor
     def self.restore(name, value)
       return value.gsub(/\+/, " ") if name == "query"
       return value
     end

     def self.match(name)
       return ".*?" if name == "first"
       return ".*"
     end
   end

   uri = Addressable::URI.parse(
     "http://example.com/search/an+example+search+query/"
   )
   match = Addressable::Template.new(
     "http://example.com/search/{query}/"
   ).match(uri, ExampleProcessor)
   match.variables
   #=> ["query"]
   match.captures
   #=> ["an example search query"]

   uri = Addressable::URI.parse("http://example.com/a/b/c/")
   match = Addressable::Template.new(
     "http://example.com/{first}/{+second}/"
   ).match(uri, ExampleProcessor)
   match.variables
   #=> ["first", "second"]
   match.captures
   #=> ["a", "b/c"]

   uri = Addressable::URI.parse("http://example.com/a/b/c/")
   match = Addressable::Template.new(
     "http://example.com/{first}{/second*}/"
   ).match(uri)
   match.variables
   #=> ["first", "second"]
   match.captures
   #=> ["a", ["b", "c"]]