Method
GLib Regexreplace_eval
since: 2.14
Declaration [src]
gchar*
g_regex_replace_eval(
constGRegex*regex,
constgchar*string,
gssizestring_len,
gintstart_position,
GRegexMatchFlagsmatch_options,
GRegexEvalCallbackeval,
gpointeruser_data,
GError**error
)
Description [src]
Replaces occurrences of the pattern in regex with the output of
eval for that occurrence.
Setting start_position differs from just passing over a shortened
string and setting G_REGEX_MATCH_NOTBOL in the case of a pattern
that begins with any kind of lookbehind assertion, such as “\b”.
The following example uses g_regex_replace_eval() to replace multiple
strings at once:
staticgboolean eval_cb(constGMatchInfo*info, GString*res, gpointerdata) { gchar*match; gchar*r; match=g_match_info_fetch(info,0); r=g_hash_table_lookup((GHashTable*)data,match); g_string_append(res,r); g_free(match); returnFALSE; } ... GRegex*reg; GHashTable*h; gchar*res; h=g_hash_table_new(g_str_hash,g_str_equal); g_hash_table_insert(h,"1","ONE"); g_hash_table_insert(h,"2","TWO"); g_hash_table_insert(h,"3","THREE"); g_hash_table_insert(h,"4","FOUR"); reg=g_regex_new("1|2|3|4",G_REGEX_DEFAULT,G_REGEX_MATCH_DEFAULT,NULL); res=g_regex_replace_eval(reg,text,-1,0,0,eval_cb,h,NULL); g_hash_table_destroy(h); ...
Available since: 2.14
Parameters
string-
Type: An array of
gcharString to perform matches against.
The length of the array is specified in thestring_lenargument.The data is owned by the caller of the method.Each element is a NUL terminated UTF-8 string. string_len-
Type:
gssizeThe length of
string, in bytes, or -1 ifstringis nul-terminated. start_position-
Type:
gintStarting index of the string to match, in bytes.
match_options-
Type:
GRegexMatchFlagsOptions for the match.
eval-
Type:
GRegexEvalCallbackA function to call for each match.
user_data-
Type:
gpointerUser data to pass to the function.
The argument can beNULL.The data is owned by the caller of the method. error-
Type:
The return location for a recoverable error.
The argument can beNULL.If the return location is notNULL, then you must initialize it to aNULLGError*.The argument will be left initialized toNULLby the method if there are no errors.In case of error, the argument will be set to a newly allocatedGError; the caller will take ownership of the data, and be responsible for freeing it.
Return value
Type: gchar*
A newly allocated string containing the replacements.