Skip to content

Instantly share code, notes, and snippets.

@ehabkost
Last active December 9, 2020 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ehabkost/35f933a86d1ba06066d638aba9019904 to your computer and use it in GitHub Desktop.
Save ehabkost/35f933a86d1ba06066d638aba9019904 to your computer and use it in GitHub Desktop.
/* Copyright (C) 2019 Red Hat Inc.
*
* Authors:
* Eduardo Habkost <ehabkost@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
char *reverse_fprintf(const char *format, const char *str)
{
char *r = NULL;
char *pattern = NULL;
GRegex *re = NULL;
GMatchInfo *match = NULL;
pattern = g_strdup_printf(format, "(.*)");
re = g_regex_new(pattern, G_REGEX_ANCHORED, 0, NULL);
if (!re) {
goto out;
}
if (!g_regex_match(re, str, 0, &match)) {
goto out;
}
r = g_strdup(g_match_info_fetch(match, 1));
out:
g_match_info_free(match);
if (re) {
g_regex_unref(re);
}
g_free(pattern);
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment