=== modified file 'includes/menu.inc'
--- includes/menu.inc	2009-05-14 08:35:58 +0000
+++ includes/menu.inc	2009-05-23 01:02:40 +0000
@@ -618,8 +618,8 @@ function _menu_translate(&$router_item, 
   // The $path_map saves the pieces of the path as strings, while elements in
   // $map may be replaced with loaded objects.
   $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;
   }
@@ -698,6 +698,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);
@@ -879,6 +883,7 @@ function menu_tree_all_data($menu_name, 
         'title_arguments',
         'type',
         'description',
+        'mask',
       ));
       for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
         $query->orderBy('p' . $i, 'ASC');
@@ -1049,6 +1054,7 @@ function menu_tree_page_data($menu_name)
           'title_arguments',
           'type',
           'description',
+          'mask',
         ));
         for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
           $query->orderBy('p' . $i, 'ASC');
@@ -2565,13 +2571,13 @@ function _menu_router_build($callbacks) 
   }
   array_multisort($sort, SORT_NUMERIC, $menu);
   // Apply inheritance rules.
-  foreach ($menu as $path => $v) {
-    $item = &$menu[$path];
+  foreach ($menu as $path => &$item) {
     if (!$item['_tab']) {
       // Non-tab items.
       $item['tab_parent'] = '';
       $item['tab_root'] = $path;
     }
+    $item['mask'] = _menu_build_mask($item, $path);
     for ($i = $item['_number_parts'] - 1; $i; $i--) {
       $parent_path = implode('/', array_slice($item['_parts'], 0, $i));
       if (isset($menu[$parent_path])) {
@@ -2666,6 +2672,7 @@ function _menu_router_save($menu, $masks
       'description',
       'position',
       'weight',
+      'mask',
     ));
 
   foreach ($menu as $path => $item) {
@@ -2690,6 +2697,7 @@ function _menu_router_save($menu, $masks
       'description' => $item['description'],
       'position' => $item['position'],
       'weight' => $item['weight'],
+      'mask' => $item['mask'],
     ));
   }
   // Execute insert object.
@@ -2701,6 +2709,72 @@ function _menu_router_save($menu, $masks
 }
 
 /**
+ * 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) {

=== modified file 'modules/simpletest/tests/menu.test'
--- modules/simpletest/tests/menu.test	2009-05-17 07:49:13 +0000
+++ modules/simpletest/tests/menu.test	2009-05-23 00:19:26 +0000
@@ -162,3 +162,103 @@ class MenuRebuildTestCase extends Drupal
   }
 
 }
+
+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	2009-05-16 20:25:19 +0000
+++ modules/system/system.install	2009-05-23 00:45:36 +0000
@@ -794,6 +794,13 @@ function system_schema() {
         'not null' => TRUE,
         'default' => 0,
       ),
+      'mask' => array(
+        'description' => 'A regular expression that should match the path.',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
+      ),
       'number_parts' => array(
         'description' => 'Number of parts in this router path.',
         'type' => 'int',
@@ -3498,6 +3505,15 @@ function system_update_7023() {
 }
 
 /**
+ * Add the 'mask' column to the {menu_router} table.
+ */
+function system_update_7024() {
+  $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	2009-05-19 19:01:51 +0000
+++ modules/taxonomy/taxonomy.module	2009-05-23 00:09:14 +0000
@@ -142,6 +142,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	2009-04-26 19:44:37 +0000
+++ modules/taxonomy/taxonomy.pages.inc	2009-05-23 00:09:14 +0000
@@ -95,9 +95,6 @@ function taxonomy_term_page($terms, $dep
 
           node_feed($nids, $channel);
           break;
-
-        default:
-          drupal_not_found();
       }
     }
     else {

