Index: wp-includes/shortcodes.php
===================================================================
--- wp-includes/shortcodes.php	(revision 17613)
+++ wp-includes/shortcodes.php	(working copy)
@@ -133,10 +133,10 @@
  * If there are no shortcode tags defined, then the content will be returned
  * without any filtering. This might cause issues when plugins are disabled but
  * the shortcode will still show up in the post or content.
+ * @see get_shortcode_regex for details of the pattern.
  *
  * @since 2.5
  * @uses $shortcode_tags
- * @uses get_shortcode_regex() Gets the search pattern for searching shortcodes.
  *
  * @param string $content Content to search for shortcodes
  * @return string Content with shortcodes filtered out.
@@ -147,11 +147,43 @@
 	if (empty($shortcode_tags) || !is_array($shortcode_tags))
 		return $content;
 
-	$pattern = get_shortcode_regex();
-	return preg_replace_callback('/'.$pattern.'/s', 'do_shortcode_tag', $content);
+	$pattern = '(.?)\[(\w+?)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)';
+	return preg_replace_callback('/'.$pattern.'/s', 'escape_shortcode', $content);
 }
 
 /**
+ * Escapes shortcodes.
+ *
+ * Because of the way 'embed' is implemented, shortcodes are escaped only during
+ * the second pass when all shortcodes are added to the shortcode_tags array.
+ *
+ * If a shortcode is not active its hook is not called.
+ * @see get_shortcode_regex for details of the match array contents.
+ *
+ * @since 3.1
+ * @uses $shortcode_tags
+ *
+ * @param array $m Regular expression match array.
+ * @return string The shortcode either escaped, unchanged or modified by its hook.
+ */
+function escape_shortcode($m){
+	global $shortcode_tags;
+
+	if( '[' == $m[1]  && ']' == $m[6] ){ 
+		//HACK! If this is the first pass of the do_shortcode() does not escape.
+		if( ! empty( $shortcode_tags['gallery'] ) )
+			return substr( $m[0], 1, -1 );
+		else
+			return $m[0];
+	}
+
+	if( ! empty( $shortcode_tags[$m[2]] ) )
+		return do_shortcode_tag($m);
+	else
+		return $m[0];	
+}
+
+/**
  * Retrieve the shortcode regular expression for searching.
  *
  * The regular expression combines the shortcode tags in the regular expression
@@ -193,11 +225,6 @@
 function do_shortcode_tag( $m ) {
 	global $shortcode_tags;
 
-	// allow [[foo]] syntax for escaping a tag
-	if ( $m[1] == '[' && $m[6] == ']' ) {
-		return substr($m[0], 1, -1);
-	}
-
 	$tag = $m[2];
 	$attr = shortcode_parse_atts( $m[3] );
 
@@ -295,4 +322,4 @@
 
 add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()
 
-?>
\ No newline at end of file
+?>
