title: Router::compile
body: <p>I'm trying to understand how a router works in the Kohana.</p>
  
  <p>I walked to the method compile and and faced with difficulties...</p>
  
  <p>What makes this line here :</p>
  
  <pre><code> $expression = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $uri);
  </code></pre>
  
  <p>Method compile:</p>
  
  <pre><code>public static function compile($uri, array $regex = NULL)
      {
          // The URI should be considered literal except for keys and optional parts
          // Escape everything preg_quote would escape except for : ( ) &lt; &gt;
          $expression = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $uri);
  
          if (strpos($expression, '(') !== FALSE)
          {
              // Make optional parts of the URI non-capturing and optional
              $expression = str_replace(array('(', ')'), array('(?:', ')?'), $expression);
          }
  
          // Insert default regex for keys
          $expression = str_replace(array('&lt;', '&gt;'), array('(?P&lt;', '&gt;'.Route::REGEX_SEGMENT.')'), $expression);
  
          if ($regex)
          {
              $search = $replace = array();
              foreach ($regex as $key =&gt; $value)
              {
                  $search[]  = "&lt;$key&gt;".Route::REGEX_SEGMENT;
                  $replace[] = "&lt;$key&gt;$value";
              }
  
              // Replace the default regex with the user-specified regex
              $expression = str_replace($search, $replace, $expression);
          }
  
          return '#^'.$expression.'$#uD';
      }
  
  
  
  const REGEX_ESCAPE  = '[.\\+*?[^\\]${}=!|]';
  </code></pre>
  
  <p>Can prompt separate article that will help me understand?</p>