GLib.Regex¶
record (struct)
A GRegex is a compiled form of a regular expression.
After instantiating a GRegex, you can use its methods to find matches
in a string, replace matches within a string, or split the string at matches.
GRegex implements regular expression pattern matching using syntax and
semantics (such as character classes, quantifiers, and capture groups)
similar to Perl regular expression. See the
PCRE documentation for details.
A typical scenario for regex pattern matching is to check if a string matches a pattern. The following statements implement this scenario.
const char *regex_pattern = ".*GLib.*";
const char *string_to_search = "You will love the GLib implementation of regex";
g_autoptr(GMatchInfo) match_info = NULL;
g_autoptr(GRegex) regex = NULL;
regex = g_regex_new (regex_pattern, G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
g_assert (regex != NULL);
if (g_regex_match (regex, string_to_search, G_REGEX_MATCH_DEFAULT, &match_info))
{
int start_pos, end_pos;
g_match_info_fetch_pos (match_info, 0, &start_pos, &end_pos);
g_print ("Match successful! Overall pattern matches bytes %d to %d\n", start_pos, end_pos);
}
else
{
g_print ("No match!\n");
}
The constructor for GRegex includes two sets of bitmapped flags:
RegexCompileFlags—These flags control how GLib compiles the regex. There are options for case sensitivity, multiline, ignoring whitespace, etc.RegexMatchFlags—These flags controlGRegex’s matching behavior, such as anchoring and customizing definitions for newline characters.
Some regex patterns include backslash assertions, such as \d (digit) or
\D (non-digit). The regex pattern must escape those backslashes. For
example, the pattern "\\d\\D" matches a digit followed by a non-digit.
GLib’s implementation of pattern matching includes a start_position
argument for some of the match, replace, and split methods. Specifying
a start position provides flexibility when you want to ignore the first
n characters of a string, but want to incorporate backslash assertions
at character n - 1. For example, a database field contains inconsistent
spelling for a job title: healthcare provider and health-care provider.
The database manager wants to make the spelling consistent by adding a
hyphen when it is missing. The following regex pattern tests for the string
care preceded by a non-word boundary character (instead of a hyphen)
and followed by a space.
An efficient way to match with this pattern is to start examining at
start_position 6 in the string healthcare or health-care.
const char *regex_pattern = "\\Bcare\\s";
const char *string_to_search = "healthcare provider";
g_autoptr(GMatchInfo) match_info = NULL;
g_autoptr(GRegex) regex = NULL;
regex = g_regex_new (
regex_pattern,
G_REGEX_DEFAULT,
G_REGEX_MATCH_DEFAULT,
NULL);
g_assert (regex != NULL);
g_regex_match_full (
regex,
string_to_search,
-1,
6, // position of 'c' in the test string.
G_REGEX_MATCH_DEFAULT,
&match_info,
NULL);
The method Regex.match_full (and other methods implementing
start_pos) allow for lookback before the start position to determine if
the previous character satisfies an assertion.
Unless you set the RegexCompileFlags.RAW as one of
the GRegexCompileFlags, all the strings passed to GRegex methods must
be encoded in UTF-8. The lengths and the positions inside the strings are
in bytes and not in characters, so, for instance, \xc3\xa0 (i.e., à)
is two bytes long but it is treated as a single character. If you set
G_REGEX_RAW, the strings can be non-valid UTF-8 strings and a byte is
treated as a character, so \xc3\xa0 is two bytes and two characters long.
Regarding line endings, \n matches a \n character, and \r matches
a \r character. More generally, \R matches all typical line endings:
CR + LF (\r\n), LF (linefeed, U+000A, \n), VT (vertical tab, U+000B,
\v), FF (formfeed, U+000C, \f), CR (carriage return, U+000D, \r),
NEL (next line, U+0085), LS (line separator, U+2028), and PS (paragraph
separator, U+2029).
The behaviour of the dot, circumflex, and dollar metacharacters are
affected by newline characters. By default, GRegex matches any newline
character matched by \R. You can limit the matched newline characters by
specifying the RegexMatchFlags.NEWLINE_CR,
RegexMatchFlags.NEWLINE_LF, and
RegexMatchFlags.NEWLINE_CRLF compile options, and
with RegexMatchFlags.NEWLINE_ANY,
RegexMatchFlags.NEWLINE_CR,
RegexMatchFlags.NEWLINE_LF and
RegexMatchFlags.NEWLINE_CRLF match options.
These settings are also relevant when compiling a pattern if
RegexCompileFlags.EXTENDED is set and an unescaped
# outside a character class is encountered. This indicates a comment
that lasts until after the next newline.
Because GRegex does not modify its internal state between creation and
destruction, you can create and modify the same GRegex instance from
different threads. In contrast, MatchInfo is not thread safe.
The regular expression low-level functionalities are obtained through the excellent PCRE library written by Philip Hazel.
Constructors¶
new¶
@classmethod
def new(cls, pattern: str, compile_options: RegexCompileFlags | int, match_options: RegexMatchFlags | int) -> Regex | None
Compiles the regular expression to an internal form, and does
the initial setup of the Regex structure.
Parameters:
pattern— the regular expressioncompile_options— compile options for the regular expression, or 0match_options— match options for the regular expression, or 0
Methods¶
get_capture_count¶
Returns the number of capturing subpatterns in the pattern.
get_compile_flags¶
Returns the compile options that regex was created with.
Depending on the version of PCRE that is used, this may or may not
include flags set by option expressions such as (?i) found at the
top-level within the compiled pattern.
get_has_cr_or_lf¶
Checks whether the pattern contains explicit CR or LF references.
get_match_flags¶
Returns the match options that regex was created with.
get_max_backref¶
Returns the number of the highest back reference in the pattern, or 0 if the pattern does not contain back references.
get_max_lookbehind¶
Gets the number of characters in the longest lookbehind assertion in the pattern. This information is useful when doing multi-segment matching using the partial matching facilities.
get_pattern¶
Gets the pattern string associated with regex, i.e. a copy of
the string passed to Regex.new.
get_string_number¶
Retrieves the number of the subexpression named name.
Parameters:
name— name of the subexpression
match_¶
Scans for a match in string for the pattern in regex.
The match_options are combined with the match options specified
when the regex structure was created, letting you have more
flexibility in reusing Regex structures.
Unless RegexCompileFlags.RAW is specified in the options, string must be valid UTF-8.
A MatchInfo structure, used to get information on the match,
is stored in match_info if not None. Note that if match_info
is not None then it is created even if the function returns False,
i.e. you must free it regardless if regular expression actually matched.
To retrieve all the non-overlapping matches of the pattern in
string you can use MatchInfo.next.
static void
print_uppercase_words (const gchar *string)
{
// Print all uppercase-only words.
GRegex *regex;
GMatchInfo *match_info;
regex = g_regex_new ("[A-Z]+", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
g_regex_match (regex, string, 0, &match_info);
while (g_match_info_matches (match_info))
{
gchar *word = g_match_info_fetch (match_info, 0);
g_print ("Found: %s\n", word);
g_free (word);
g_match_info_next (match_info, NULL);
}
g_match_info_free (match_info);
g_regex_unref (regex);
}
string is not copied and is used in MatchInfo internally. If
you use any MatchInfo method (except MatchInfo.free) after
freeing or modifying string then the behaviour is undefined.
Parameters:
string— the string to scan for matchesmatch_options— match options
match_all¶
Using the standard algorithm for regular expression matching only
the longest match in the string is retrieved. This function uses
a different algorithm so it can retrieve all the possible matches.
For more documentation see Regex.match_all_full.
A MatchInfo structure, used to get information on the match, is
stored in match_info if not None. Note that if match_info is
not None then it is created even if the function returns False,
i.e. you must free it regardless if regular expression actually
matched.
string is not copied and is used in MatchInfo internally. If
you use any MatchInfo method (except MatchInfo.free) after
freeing or modifying string then the behaviour is undefined.
Parameters:
string— the string to scan for matchesmatch_options— match options
match_all_full¶
def match_all_full(self, string: list[str], start_position: int, match_options: RegexMatchFlags | int) -> tuple[bool, MatchInfo]
Using the standard algorithm for regular expression matching only
the longest match in the string is retrieved, it is not possible
to obtain all the available matches. For instance matching
"<a> <b> <c>" against the pattern "<.*>"
you get "<a> <b> <c>".
This function uses a different algorithm (called DFA, i.e. deterministic
finite automaton), so it can retrieve all the possible matches, all
starting at the same point in the string. For instance matching
"<a> <b> <c>" against the pattern "<.*>"
you would obtain three matches: "<a> <b> <c>",
"<a> <b>" and "<a>".
The number of matched strings is retrieved using
MatchInfo.get_match_count. To obtain the matched strings and
their position you can use, respectively, MatchInfo.fetch and
MatchInfo.fetch_pos. Note that the strings are returned in
reverse order of length; that is, the longest matching string is
given first.
Note that the DFA algorithm is slower than the standard one and it is not able to capture substrings, so backreferences do not work.
Setting start_position differs from just passing over a shortened
string and setting RegexMatchFlags.NOTBOL in the case of a pattern
that begins with any kind of lookbehind assertion, such as "\b".
Unless RegexCompileFlags.RAW is specified in the options, string must be valid UTF-8.
A MatchInfo structure, used to get information on the match, is
stored in match_info if not None. Note that if match_info is
not None then it is created even if the function returns False,
i.e. you must free it regardless if regular expression actually
matched.
string is not copied and is used in MatchInfo internally. If
you use any MatchInfo method (except MatchInfo.free) after
freeing or modifying string then the behaviour is undefined.
Parameters:
string— the string to scan for matchesstart_position— starting index of the string to match, in bytesmatch_options— match options
match_full¶
def match_full(self, string: list[str], start_position: int, match_options: RegexMatchFlags | int) -> tuple[bool, MatchInfo]
Scans for a match in string for the pattern in regex.
The match_options are combined with the match options specified
when the regex structure was created, letting you have more
flexibility in reusing Regex structures.
Setting start_position differs from just passing over a shortened
string and setting RegexMatchFlags.NOTBOL in the case of a pattern
that begins with any kind of lookbehind assertion, such as "\b".
Unless RegexCompileFlags.RAW is specified in the options, string must be valid UTF-8.
A MatchInfo structure, used to get information on the match, is
stored in match_info if not None. Note that if match_info is
not None then it is created even if the function returns False,
i.e. you must free it regardless if regular expression actually
matched.
string is not copied and is used in MatchInfo internally. If
you use any MatchInfo method (except MatchInfo.free) after
freeing or modifying string then the behaviour is undefined.
To retrieve all the non-overlapping matches of the pattern in
string you can use MatchInfo.next.
static void
print_uppercase_words (const gchar *string)
{
// Print all uppercase-only words.
GRegex *regex;
GMatchInfo *match_info;
GError *error = NULL;
regex = g_regex_new ("[A-Z]+", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
g_regex_match_full (regex, string, -1, 0, 0, &match_info, &error);
while (g_match_info_matches (match_info))
{
gchar *word = g_match_info_fetch (match_info, 0);
g_print ("Found: %s\n", word);
g_free (word);
g_match_info_next (match_info, &error);
}
g_match_info_free (match_info);
g_regex_unref (regex);
if (error != NULL)
{
g_printerr ("Error while matching: %s\n", error->message);
g_error_free (error);
}
}
Parameters:
string— the string to scan for matchesstart_position— starting index of the string to match, in bytesmatch_options— match options
ref¶
Increases reference count of regex by 1.
replace¶
def replace(self, string: list[str], start_position: int, replacement: str, match_options: RegexMatchFlags | int) -> str
Replaces all occurrences of the pattern in regex with the
replacement text. Backreferences of the form \number or
\g<number> in the replacement text are interpolated by the
number-th captured subexpression of the match, \g<name> 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 Regex.replace_literal.
The replacement string must be UTF-8 encoded even if RegexCompileFlags.RAW was
passed to Regex.new. If you want to use not UTF-8 encoded strings
you can use Regex.replace_literal.
Setting start_position differs from just passing over a shortened
string and setting RegexMatchFlags.NOTBOL in the case of a pattern that
begins with any kind of lookbehind assertion, such as "\b".
Parameters:
string— the string to perform matches againststart_position— starting index of the string to match, in bytesreplacement— text to replace each match withmatch_options— options for the match
replace_eval¶
def replace_eval(self, string: list[str], start_position: int, match_options: RegexMatchFlags | int, eval: RegexEvalCallback) -> str
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 RegexMatchFlags.NOTBOL in the case of a pattern
that begins with any kind of lookbehind assertion, such as "\b".
The following example uses Regex.replace_eval to replace multiple
strings at once:
static gboolean
eval_cb (const GMatchInfo *info,
GString *res,
gpointer data)
{
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);
return FALSE;
}
...
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);
...
Parameters:
string— string to perform matches againststart_position— starting index of the string to match, in bytesmatch_options— options for the matcheval— a function to call for each match
replace_literal¶
def replace_literal(self, string: list[str], start_position: int, replacement: str, match_options: RegexMatchFlags | int) -> str
Replaces all occurrences of the pattern in regex with the
replacement text. replacement is replaced literally, to
include backreferences use Regex.replace.
Setting start_position differs from just passing over a
shortened string and setting RegexMatchFlags.NOTBOL in the
case of a pattern that begins with any kind of lookbehind
assertion, such as "\b".
Parameters:
string— the string to perform matches againststart_position— starting index of the string to match, in bytesreplacement— text to replace each match withmatch_options— options for the match
split¶
Breaks the string on the pattern, and returns an array of the tokens. If the pattern contains capturing parentheses, then the text for each of the substrings will also be returned. If the pattern does not match anywhere in the string, then the whole string is returned as the first token.
As a special case, the result of splitting the empty string "" is an empty vector, not a vector containing a single string. The reason for this special case is that being able to represent an empty vector is typically more useful than consistent handling of empty elements. If you do need to represent empty elements, you'll need to check for the empty string before calling this function.
A pattern that can match empty strings splits string into separate
characters wherever it matches the empty string between characters.
For example splitting "ab c" using as a separator "\s*", you will get
"a", "b" and "c".
Parameters:
string— the string to split with the patternmatch_options— match time option flags
split_full¶
def split_full(self, string: list[str], start_position: int, match_options: RegexMatchFlags | int, max_tokens: int) -> list[str]
Breaks the string on the pattern, and returns an array of the tokens. If the pattern contains capturing parentheses, then the text for each of the substrings will also be returned. If the pattern does not match anywhere in the string, then the whole string is returned as the first token.
As a special case, the result of splitting the empty string "" is an empty vector, not a vector containing a single string. The reason for this special case is that being able to represent an empty vector is typically more useful than consistent handling of empty elements. If you do need to represent empty elements, you'll need to check for the empty string before calling this function.
A pattern that can match empty strings splits string into separate
characters wherever it matches the empty string between characters.
For example splitting "ab c" using as a separator "\s*", you will get
"a", "b" and "c".
Setting start_position differs from just passing over a shortened
string and setting RegexMatchFlags.NOTBOL in the case of a pattern
that begins with any kind of lookbehind assertion, such as "\b".
Parameters:
string— the string to split with the patternstart_position— starting index of the string to match, in bytesmatch_options— match time option flagsmax_tokens— the maximum number of tokens to splitstringinto. If this is less than 1, the string is split completely
unref¶
Decreases reference count of regex by 1. When reference count drops
to zero, it frees all the memory associated with the regex structure.
Static functions¶
check_replacement¶
Checks whether replacement is a valid replacement string
(see Regex.replace), i.e. that all escape sequences in
it are valid.
If has_references is not None then replacement is checked
for pattern references. For instance, replacement text 'foo\n'
does not contain references and may be evaluated without information
about actual match, but '\0\1' (whole match followed by first
subpattern) requires valid MatchInfo object.
Parameters:
replacement— the replacement string
error_quark¶
escape_nul¶
Escapes the nul characters in string to "\x00". It can be used
to compile a regex with embedded nul characters.
For completeness, length can be -1 for a nul-terminated string.
In this case the output string will be of course equal to string.
Parameters:
string— the string to escapelength— the length ofstring
escape_string¶
Escapes the special characters used for regular expressions
in string, for instance "a.b*c" becomes "a.b*c". This
function is useful to dynamically generate regular expressions.
string can contain nul characters that are replaced with "\0",
in this case remember to specify the correct length of string
in length.
Parameters:
string— the string to escapelength— the length ofstring, in bytes, or -1 ifstringis nul-terminated
match_simple¶
@staticmethod
def match_simple(pattern: str, string: str, compile_options: RegexCompileFlags | int, match_options: RegexMatchFlags | int) -> bool
Scans for a match in string for pattern.
This function is equivalent to Regex.match_ but it does not
require to compile the pattern with Regex.new, avoiding some
lines of code when you need just to do a match without extracting
substrings, capture counts, and so on.
If this function is to be called on the same pattern more than
once, it's more efficient to compile the pattern once with
Regex.new and then use Regex.match_.
Parameters:
pattern— the regular expressionstring— the string to scan for matchescompile_options— compile options for the regular expression, or 0match_options— match options, or 0
split_simple¶
@staticmethod
def split_simple(pattern: str, string: str, compile_options: RegexCompileFlags | int, match_options: RegexMatchFlags | int) -> list[str]
Breaks the string on the pattern, and returns an array of the tokens. If the pattern contains capturing parentheses, then the text for each of the substrings will also be returned. If the pattern does not match anywhere in the string, then the whole string is returned as the first token.
This function is equivalent to Regex.split but it does
not require to compile the pattern with Regex.new, avoiding
some lines of code when you need just to do a split without
extracting substrings, capture counts, and so on.
If this function is to be called on the same pattern more than
once, it's more efficient to compile the pattern once with
Regex.new and then use Regex.split.
As a special case, the result of splitting the empty string "" is an empty vector, not a vector containing a single string. The reason for this special case is that being able to represent an empty vector is typically more useful than consistent handling of empty elements. If you do need to represent empty elements, you'll need to check for the empty string before calling this function.
A pattern that can match empty strings splits string into
separate characters wherever it matches the empty string between
characters. For example splitting "ab c" using as a separator
"\s*", you will get "a", "b" and "c".
Parameters:
pattern— the regular expressionstring— the string to scan for matchescompile_options— compile options for the regular expression, or 0match_options— match options, or 0