=== modified file 'includes/menu.inc'
--- includes/menu.inc	2008-07-10 10:58:01 +0000
+++ includes/menu.inc	2008-08-15 09:02:11 +0000
@@ -603,8 +603,8 @@ function _menu_item_localize(&$item, $ma
  */
 function _menu_translate(&$router_item, $map, $to_arg = FALSE) {
   $path_map = $map;
-  if (!_menu_load_objects($router_item, $map)) {
-    // An error occurred loading an object.
+  if (!preg_match($router_item['mask'], implode($map, '/')) || !_menu_load_objects($router_item, $map)) {
+    // Either the path did not validate or an error occurred loading an object.
     $router_item['access'] = FALSE;
     return FALSE;
   }
@@ -686,6 +686,10 @@ function _menu_link_translate(&$item) {
     $item['localized_options'] = $item['options'];
   }
   else {
+    if (!preg_match($item['mask'], $item['link_path'])) {
+      $item['access'] = FALSE;
+      return FALSE;
+    }
     $map = explode('/', $item['link_path']);
     _menu_link_map_translate($map, $item['to_arg_functions']);
     $item['href'] = implode('/', $map);
@@ -870,7 +874,7 @@ function menu_tree_all_data($menu_name =
       // LEFT JOIN since there is no match in {menu_router} for an external
       // link.
       $data['tree'] = menu_tree_data(db_query("
-        SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
+        SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, m.mask, ml.*
         FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
         WHERE ml.menu_name = '%s'" . $where . "
         ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args), $parents);
@@ -977,7 +981,7 @@ function menu_tree_page_data($menu_name 
         // LEFT JOIN since there is no match in {menu_router} for an external
         // link.
         $data['tree'] = menu_tree_data(db_query("
-          SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
+          SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, m.mask, ml.*
           FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
           WHERE ml.menu_name = '%s' AND ml.plid IN (" . $placeholders . ")
           ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args), $parents);
@@ -2388,24 +2392,24 @@ function _menu_router_build($callbacks) 
       'tab_root' => $path,
       'path' => $path,
     );
-
+    $item['mask'] = _menu_build_mask($item, $path);
     $title_arguments = $item['title arguments'] ? serialize($item['title arguments']) : '';
     db_query("INSERT INTO {menu_router}
       (path, load_functions, to_arg_functions, access_callback,
       access_arguments, page_callback, page_arguments, fit,
       number_parts, tab_parent, tab_root,
       title, title_callback, title_arguments,
-      type, block_callback, description, position, weight)
+      type, block_callback, description, position, weight, mask)
       VALUES ('%s', '%s', '%s', '%s',
       '%s', '%s', '%s', %d,
       %d, '%s', '%s',
       '%s', '%s', '%s',
-      %d, '%s', '%s', '%s', %d)",
+      %d, '%s', '%s', '%s', %d, '%s')",
       $path, $item['load_functions'], $item['to_arg_functions'], $item['access callback'],
       serialize($item['access arguments']), $item['page callback'], serialize($item['page arguments']), $item['_fit'],
       $item['_number_parts'], $item['tab_parent'], $item['tab_root'],
       $item['title'], $item['title callback'], $title_arguments,
-      $item['type'], $item['block callback'], $item['description'], $item['position'], $item['weight']);
+      $item['type'], $item['block callback'], $item['description'], $item['position'], $item['weight'], $item['mask']);
   }
   // Sort the masks so they are in order of descending fit, and store them.
   $masks = array_keys($masks);
@@ -2416,6 +2420,72 @@ function _menu_router_build($callbacks) 
 }
 
 /**
+ * Build the mask for a menu item based on its definition.
+ *
+ * @param $item
+ *   The menu item to determine the mask of, as an array.
+ * @param $path
+ *   The path of the current menu item, e.g. node/%.
+ * @return
+ *   The string mask.
+ */
+function _menu_build_mask($item, $path) {
+  if (isset($item['mask'])) {
+    if (isset($item['mask']['rest'])) {
+      $rest = $item['mask']['rest'];
+      unset($item['mask']['rest']);
+    }
+    // If the mask is only for a wildcard inside the router path, we still
+    // need to fill the mask to covert all router parts.
+    $mask_length = $item['_number_parts'] - 1;
+    if (count($item['mask'])) {
+      $mask_length = max($mask_length, max(array_keys($item['mask'])));
+    }
+    $suffix = '';
+    $mask = '';
+    $parts = $item['_parts'];
+    for ($i = 0; $i <= $mask_length; $i++) {
+      $fallback = isset($parts[$i]) ? preg_quote($parts[$i], '#') : '';
+      $mask_part = isset($item['mask'][$i]) ? $item['mask'][$i] : $fallback;
+      _menu_build_mask_part($mask_part);
+      if (isset($parts[$i])) {
+        $prefix = $i ? '/' : '';
+      }
+      else {
+        // If we are past the last router path part then all the parts are
+        // optional, so we wrap them in conditional non grouping blocks.
+        $prefix = '(?:/';
+        $suffix .= ')?';
+      }
+      $mask .= $prefix . $mask_part;
+    }
+    $mask = '#^'. $mask;
+    if (isset($rest)) {
+      _menu_build_mask_part($rest);
+      $mask .= "(?:/$rest)*";
+    }
+    $mask .= $suffix . '$#';
+  }
+  else {
+    $mask = '#^'. str_replace('%', '\d+', preg_quote($path, '#')) . '$#';
+  }
+  return $mask;
+}
+
+function _menu_build_mask_part(&$mask_part) {
+  static $mask_map = array('*' => '[^/]+', '%' => '\d+', '%d' => '\d+', '%c' => '\w+');
+  if (isset($mask_map[$mask_part])) {
+    $mask_part = $mask_map[$mask_part];
+  }
+  elseif (preg_match('/^%([a-z_]+)$/', $mask_part, $matches)) {
+    $constant = strtoupper($matches[1]) . '_MASK';
+    if (defined($constant)) {
+      $mask_part = constant($constant);
+    }
+  }
+}
+
+/**
  * Returns TRUE if a path is external (e.g. http://example.com).
  */
 function menu_path_is_external($path) {

=== added file 'modules/simpletest/tests/menu.test'
--- modules/simpletest/tests/menu.test	1970-01-01 00:00:00 +0000
+++ modules/simpletest/tests/menu.test	2008-08-15 09:04:12 +0000
@@ -0,0 +1,102 @@
+<?php
+// $Id$
+
+class MenuMasksTestCase extends DrupalWebTestCase {
+
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Menu masks'),
+      'description' => t('Tests the menu item masking functionality, making sure it works as expected in all cases.'),
+      // Don't use 'Menu' as the group in order to keep it separate from the
+      // menu module tests.
+      'group' => t('Menu system'),
+    );
+  }
+
+  /**
+   * Tests a set of masks to make sure they work properly.
+   */
+  function testMenuMasks() {
+    // This constant is used for the %testing_test placeholder in tests 8 - 12.
+    define('TESTING_TEST_MASK', '(test|testing)');
+
+    $items = array();
+
+    $items['testing'] = array(
+      'expected' => '#^testing$#',
+    );
+
+    $items['test#ing2'] = array(
+      'expected' => '#^test\#ing2$#',
+    );
+
+    $items['testing3/%'] = array(
+      'expected' => '#^testing3/\d+$#',
+    );
+
+    $items['testing4/%'] = array(
+      'mask' => array(1 => '*'),
+      'expected' => '#^testing4/[^/]+$#',
+    );
+
+    $items['testing5/%'] = array(
+      'mask' => array(1 => '%d'),
+      'expected' => '#^testing5/\d+$#',
+    );
+
+    $items['testing6/%'] = array(
+      'mask' => array(1 => '%c'),
+      'expected' => '#^testing6/\w+$#',
+    );
+
+    $items['testing7'] = array(
+      'mask' => array(1 => '[0-9a-f]{32}'),
+      'expected' => '#^testing7(?:/[0-9a-f]{32})?$#',
+    );
+
+    $items['testing8'] = array(
+      'mask' => array(1 => '%testing_test'),
+      'expected' => '#^testing8(?:/(test|testing))?$#',
+    );
+
+    $items['testing9'] = array(
+      'mask' => array('testing9', '%d', '%testing_test'),
+      'expected' => '#^testing9(?:/\d+(?:/(test|testing))?)?$#',
+    );
+
+    $items['testing10/%'] = array(
+      'mask' => array(1 => '%d', '%testing_test'),
+      'expected' => '#^testing10/\d+(?:/(test|testing))?$#',
+    );
+
+    $items['testing11/%/%'] = array(
+      'mask' => array(1 => '%d', '%testing_test'),
+      'expected' => '#^testing11/\d+/(test|testing)$#',
+    );
+
+    $items['testing12/%/%'] = array(
+      'mask' => array(2 => '%testing_test'),
+      'expected' => '#^testing12/\d+/(test|testing)$#',
+    );
+
+    $items['testing13'] = array(
+      'mask' => array('rest' => '%d'),
+      'expected' => '#^testing13(?:/\d+)*$#',
+    );
+
+    $items['testing14'] = array(
+      'mask' => array(1 => '%d', 'rest' => '%d'),
+      'expected' => '#^testing14(?:/\d+(?:/\d+)*)? #',
+    );
+
+    foreach ($items as $path => $item) {
+      $item['_parts'] = explode('/', $path);
+      $item['_number_parts'] = count($item['_parts']);
+      $result = _menu_build_mask($item, $path);
+      $this->assertEqual($result, $item['expected'], t('Expecting menu mask to be @expected, got @result.', array('@expected' => $item['expected'], '@result' => $result)));
+    }
+  }
+}
\ No newline at end of file

=== modified file 'modules/system/system.install'
--- modules/system/system.install	2008-08-14 12:59:05 +0000
+++ modules/system/system.install	2008-08-15 09:01:14 +0000
@@ -880,6 +880,13 @@ function system_schema() {
         'not null' => TRUE,
         'default' => 0,
       ),
+      'mask' => array(
+        'description' => t('A regular expression that the url must match in order to match this menu item.'),
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
+      ),
     ),
     'indexes' => array(
       'fit' => array('fit'),
@@ -3042,6 +3049,15 @@ function system_update_7009() {
 }
 
 /**
+ * Add the 'mask' column to the {menu_router} table.
+ */
+function system_update_7010() {
+  $ret = array();
+  db_add_field($ret, 'menu_router', 'mask', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
+  return $ret;
+}
+
+/**
  * @} End of "defgroup updates-6.x-to-7.x"
  * The next series of updates should start at 8000.
  */

=== modified file 'modules/taxonomy/taxonomy.module'
--- modules/taxonomy/taxonomy.module	2008-07-24 16:25:17 +0000
+++ modules/taxonomy/taxonomy.module	2008-08-15 09:01:14 +0000
@@ -154,6 +154,7 @@ function taxonomy_menu() {
     'page callback' => 'taxonomy_term_page',
     'page arguments' => array(2),
     'access arguments' => array('access content'),
+    'mask' =>  array(2 => '(?:(?:(?:(?:\d+[+ ])+)|(?:(?:\d+,)*))\d+)', '%d', '(?:feed|page)'),
     'type' => MENU_CALLBACK,
   );
 

=== modified file 'modules/taxonomy/taxonomy.pages.inc'
--- modules/taxonomy/taxonomy.pages.inc	2008-04-14 17:48:33 +0000
+++ modules/taxonomy/taxonomy.pages.inc	2008-08-15 09:01:14 +0000
@@ -64,9 +64,6 @@ function taxonomy_term_page($str_tids = 
 
           node_feed($items, $channel);
           break;
-
-        default:
-          drupal_not_found();
       }
     }
     else {

