diff --git a/glib/gregex.c b/glib/gregex.c
index 091b418..ffc31b6 100644
--- a/glib/gregex.c
+++ b/glib/gregex.c
@@ -820,12 +820,19 @@ g_match_info_fetch_pos (const GMatchInfo *match_info,
 			gint             *start_pos,
 			gint             *end_pos)
 {
+  gint capture_count;
+
   g_return_val_if_fail (match_info != NULL, FALSE);
   g_return_val_if_fail (match_num >= 0, FALSE);
- 
-  /* make sure the sub expression number they're requesting is less than
-   * the total number of sub expressions that were matched. */
-  if (match_num >= match_info->matches)
+
+  pcre_fullinfo (match_info->regex->pcre_re,
+                 match_info->regex->extra,
+                 PCRE_INFO_CAPTURECOUNT, &capture_count);
+
+  /* Make sure the sub expression number they're requesting is less
+   * than the total number of sub expressions that were matched.
+   */
+  if (match_num >= MAX (capture_count + 1, match_info->matches))
     return FALSE;
 
   if (start_pos != NULL)
