since: 2.14


g_regex_replace (
  const GRegex* regex,
  const gchar* string,
  gssize string_len,
  gint start_position,
  const gchar* replacement,
  GRegexMatchFlags match_options,
  GError** error


Replaces all occurrences of the pattern in regex with the replacement text. Backreferences of the form ‘\number’ or ‘\g‘ in the replacement text are interpolated by the number-th captured subexpression of the match, ‘\g‘ refers to the captured subexpression with the given name. ‘\0’ refers to the complete match, but ‘\0’ followed by a number is the octal representation of a character. To include a literal ‘' in the replacement, write ‘\’.

There are also escapes that changes the case of the following text:

  • \l: Convert to lower case the next character
  • \u: Convert to upper case the next character
  • \L: Convert to lower case till \E
  • \U: Convert to upper case till \E
  • \E: End case modification

If you do not need to use backreferences use g_regex_replace_literal().

The replacement string must be UTF-8 encoded even if G_REGEX_RAW was passed to g_regex_new(). If you want to use not UTF-8 encoded strings you can use g_regex_replace_literal().

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”.

Available since: 2.14



Type: An array of gchar

The string to perform matches against.

The length of the array is specified in the string_len argument.
The data is owned by the caller of the function.
Each element is a NUL terminated UTF-8 string.

Type: gssize

The length of string, in bytes, or -1 if string is nul-terminated.


Type: gint

Starting index of the string to match, in bytes.


Type: const gchar*

Text to replace each match with.

The data is owned by the caller of the function.
The value is a NUL terminated UTF-8 string.

Type: GRegexMatchFlags

Options for the match.


Type: GError **

The return location for a recoverable error.

The argument can be NULL.
If the return location is not NULL, then you must initialize it to a NULL GError*.
The argument will left initialized to NULL by the method if there are no errors.
In case of error, the argument will be set to a newly allocated GError; 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.

The caller of the method takes ownership of the data, and is responsible for freeing it.
The value is a NUL terminated UTF-8 string.