#include <glib.h>
#include <stdlib.h>

#define LEN_SPEC "\\d"
#define PATTERN "(?P<len1>"LEN_SPEC")(?P<len2>"LEN_SPEC")?"
#define TO_MATCH "1"

gint main ()
{
    const gchar reg[] = PATTERN;
    GRegex *pattern = NULL;
    gchar to_match[] = TO_MATCH;
    GMatchInfo *m_info = NULL;
    
    GError *err = NULL;
    
    g_print ("Create regex: %s\n", reg);
    pattern = g_regex_new (reg, 0, 0, &err);
    
    if (err) {
        g_print ("failed to create regex\n");
        exit (-1);
    }
    
    g_print ("Match \"%s\"\n", to_match);
    if (!g_regex_match (pattern, to_match, 0, &m_info)) {
            g_warning ("Bad parameter: \"%s\"\n", to_match);
            goto out;
    }
    
    {
        gchar *len1, *len2;
        len1 = g_match_info_fetch_named (m_info, "len1");
        len2 = g_match_info_fetch_named (m_info, "len2");
        
        g_print ("len1: \"%s\", len2: \"%s\"\n", len1, len2);
    }

out: 
    if (m_info)
        g_match_info_free (m_info);
    
    return 0;
}
