diff --git a/gst/subparse/gstsubparse.c b/gst/subparse/gstsubparse.c
index bba1e08..88e8107 100644
--- a/gst/subparse/gstsubparse.c
+++ b/gst/subparse/gstsubparse.c
@@ -26,7 +26,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
-#include <regex.h>
+#include <glib.h>
 
 #include "gstsubparse.h"
 #include "gstssaparse.h"
@@ -969,6 +969,75 @@ parser_state_dispose (ParserState * state)
 #endif
 }
 
+/* regex type enum */
+typedef enum
+{
+  GST_SUB_PARSE_REGEX_UNKNOWN = 0,
+  GST_SUB_PARSE_REGEX_MDVDSUB = 1,
+  GST_SUB_PARSE_REGEX_SUBRIP = 2,
+} GstSubParseRegex;
+
+static gpointer
+gst_sub_parse_data_format_autodetect_regex_once (GstSubParseRegex regtype)
+{
+  gpointer result = NULL;
+#if GLIB_CHECK_VERSION(2,16,0)
+  GError *gerr = NULL;
+  switch (regtype)
+  {
+    case GST_SUB_PARSE_REGEX_MDVDSUB:
+      result = (gpointer) g_regex_new ("^\\{[0-9]+\\}\\{[0-9]+\\}", 0, 0, &gerr);
+      if (result == NULL) {
+        g_warning ("Compilation of mdvd regex failed: %s", gerr->message);
+        g_error_free (gerr);
+      }
+    break;
+    case GST_SUB_PARSE_REGEX_SUBRIP:
+      result = (gpointer) g_regex_new ("^([ 0-9]){0,3}[0-9](\x0d)?\x0a"
+                "[ 0-9][0-9]:[ 0-9][0-9]:[ 0-9][0-9],[ 0-9]{2}[0-9]"
+                " --> ([ 0-9])?[0-9]:[ 0-9][0-9]:[ 0-9][0-9],[ 0-9]{2}[0-9]",
+                0, 0, &gerr);
+      if (result == NULL) {
+        g_warning ("Compilation of subrip regex failed: %s", gerr->message);
+        g_error_free (gerr);
+      }
+    break;
+    default:
+#else
+  gint err;
+  char errstr[128];
+
+  result = (gpointer) g_try_new0(regex_t, 1);
+  if (pointer == NULL) {
+    g_warning ("Out of memory");
+  }
+
+  switch (regtype)
+  {
+    case GST_SUB_PARSE_REGEX_MDVDSUB:
+      else if ((err = regcomp (mdvd_rx, "^\\{[0-9]+\\}\\{[0-9]+\\}",
+                  REG_EXTENDED | REG_NEWLINE | REG_NOSUB) != 0) {
+        regerror (err, &subrip_rx, errstr, 127);
+        g_warning ("Compilation of mdvd regex failed: %s", errstr);
+      }
+    break;
+    case GST_SUB_PARSE_REGEX_SUBRIP:
+      if ((err = regcomp (subrip_rx, "^([ 0-9]){0,3}[0-9](\x0d)?\x0a"
+                  "[ 0-9][0-9]:[ 0-9][0-9]:[ 0-9][0-9],[ 0-9]{2}[0-9]"
+                  " --> ([ 0-9])?[0-9]:[ 0-9][0-9]:[ 0-9][0-9],[ 0-9]{2}[0-9]",
+                  REG_EXTENDED | REG_NEWLINE | REG_NOSUB)) != 0) { 
+        regerror (err, &subrip_rx, errstr, 127);
+        g_warning ("Compilation of subrip regex failed: %s", errstr);
+      }
+    break;
+    default:
+      g_free (pointer);
+#endif
+      GST_WARNING ("Trying to allocate regex of unknown type %u", regtype);
+  }
+  return result;
+}
+
 /*
  * FIXME: maybe we should pass along a second argument, the preceding
  * text buffer, because that is how this originally worked, even though
@@ -978,36 +1047,49 @@ parser_state_dispose (ParserState * state)
 static GstSubParseFormat
 gst_sub_parse_data_format_autodetect (gchar * match_str)
 {
-  static gboolean need_init_regexps = TRUE;
-  static regex_t mdvd_rx;
-  static regex_t subrip_rx;
   guint n1, n2, n3;
 
-  /* initialize the regexps used the first time around */
-  if (need_init_regexps) {
-    int err;
-    char errstr[128];
+  static GOnce mdvd_rx_once = G_ONCE_INIT;
+  static GOnce subrip_rx_once = G_ONCE_INIT;
 
-    need_init_regexps = FALSE;
-    if ((err = regcomp (&mdvd_rx, "^\\{[0-9]+\\}\\{[0-9]+\\}",
-                REG_EXTENDED | REG_NEWLINE | REG_NOSUB) != 0) ||
-        (err = regcomp (&subrip_rx, "^([ 0-9]){0,3}[0-9](\x0d)?\x0a"
-                "[ 0-9][0-9]:[ 0-9][0-9]:[ 0-9][0-9],[ 0-9]{2}[0-9]"
-                " --> ([ 0-9])?[0-9]:[ 0-9][0-9]:[ 0-9][0-9],[ 0-9]{2}[0-9]",
-                REG_EXTENDED | REG_NEWLINE | REG_NOSUB)) != 0) {
-      regerror (err, &subrip_rx, errstr, 127);
-      GST_WARNING ("Compilation of subrip regex failed: %s", errstr);
-    }
+#if GLIB_CHECK_VERSION(2,16,0)
+  GRegex *mdvd_grx;
+  GRegex *subrip_grx;
+#else
+  regex_t *mdvd_rx;
+  regex_t *subrip_rx;
+#endif
+
+  g_once(&mdvd_rx_once, (GThreadFunc) gst_sub_parse_data_format_autodetect_regex_once,
+          (gpointer) GST_SUB_PARSE_REGEX_MDVDSUB);
+  g_once(&subrip_rx_once, (GThreadFunc) gst_sub_parse_data_format_autodetect_regex_once,
+          (gpointer) GST_SUB_PARSE_REGEX_SUBRIP);
+
+#if GLIB_CHECK_VERSION(2,16,0)
+  mdvd_grx = (GRegex *) mdvd_rx_once.retval;
+  subrip_grx = (GRegex *) subrip_rx_once.retval;
+
+  if (g_regex_match (mdvd_grx, match_str, 0, NULL) == TRUE) {
+    GST_LOG ("MicroDVD (frame based) format detected");
+    return GST_SUB_PARSE_FORMAT_MDVDSUB;
   }
+  if (g_regex_match (subrip_grx, match_str, 0, NULL) == TRUE) {
+    GST_LOG ("SubRip (time based) format detected");
+    return GST_SUB_PARSE_FORMAT_SUBRIP;
+  }
+#else
+  mdvd_rx = (regex_t *) mdvd_rx_once.retval;
+  subrip_rx = (regex_t *) subrip_rx_once.retval; 
 
-  if (regexec (&mdvd_rx, match_str, 0, NULL, 0) == 0) {
+  if (regexec (mdvd_rx, match_str, 0, NULL, 0) == 0) {
     GST_LOG ("MicroDVD (frame based) format detected");
     return GST_SUB_PARSE_FORMAT_MDVDSUB;
   }
-  if (regexec (&subrip_rx, match_str, 0, NULL, 0) == 0) {
+  if (regexec (subrip_rx, match_str, 0, NULL, 0) == 0) {
     GST_LOG ("SubRip (time based) format detected");
     return GST_SUB_PARSE_FORMAT_SUBRIP;
   }
+#endif /* GLIB_CHECK_VERSION(2,16,0) */
 
   if (!strncmp (match_str, "FORMAT=TIME", 11)) {
     GST_LOG ("MPSub (time based) format detected");