replace_literal


Description:

public string replace_literal (string str, ssize_t string_len, int start_position, string replacement, RegexMatchFlags match_options = 0) throws RegexError

Replaces all occurrences of the pattern in this with the replacement text.

replacement is replaced literally, to include backreferences use replace.

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

Example: Replace literal:

public int main (string[] args) {
try {
string str = "If I had some duck tape, I could fix that.";
string old = "duck";
string replacement = "duct";

var regex = new GLib.Regex (GLib.Regex.escape_string (old));
string result = regex.replace_literal (str, -1, 0, replacement);
// Output:
// ``str: "If I had some duck tape, I could fix that."``
// ``old: "duck"``
// ``replacement: "duct"``
// ``result: "If I had some duct tape, I could fix that."``
print ("str: \"%s\"\n", str);
print ("old: \"%s\"\n", old);
print ("replacement: \"%s\"\n", replacement);
print ("result: \"%s\"\n", result);
} catch (GLib.RegexError e) {
GLib.assert_not_reached ();
}

return 0;
}

valac --pkg glib-2.0 GLib.Regex.replace_literal.vala

Parameters:

this

a Regex structure

string_len

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

start_position

starting index of the string to match, in bytes

replacement

text to replace each match with

match_options

options for the match

string

the string to perform matches against

Returns:

a newly allocated string containing the replacements