I’m looking at the return values of GLib>MatchInfo>fetch_pos. It seems like the value for the returned end_pos
is one higher than expected.
For example, I have the following string
I never knew GLib had regex
012345678901234567890123456
0 1 2
and a regex .*(knew).*
- Applying
g_match_info_fetch_pos
on capture group 0 (entire string) returns an end position of 27 (one longer than the string). - Applying
g_match_info_fetch_pos
on capture group 1 returns an end position of 12 (one longer than the capture group)
Below is the code and output.
#include <glib-2.0/glib.h>
/* Compile command
gcc post.c -lglib-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
*/
int main(int argc, char *argv[]) {
GError *error = NULL;
gchar *regex_pattern = ".*(knew).*";
gchar *test_string = "I never knew GLib had regex";
GMatchInfo *match_info;
GRegex *regex = g_regex_new (
regex_pattern,
G_REGEX_DEFAULT,
G_REGEX_MATCH_DEFAULT,
NULL);
g_regex_match (regex, test_string, G_REGEX_MATCH_DEFAULT, &match_info);
gint start_pos;
gint end_pos;
g_match_info_fetch_pos ( match_info, 0, &start_pos, &end_pos);
g_print("Start/end for position 0: %d/%d\n", start_pos, end_pos);
g_match_info_fetch_pos ( match_info, 1, &start_pos, &end_pos);
g_print("Start/end for position 1: %d/%d\n", start_pos, end_pos);
g_regex_unref(regex);
return 0;
}
Start/end for position 0: 0/27
Start/end for position 1: 8/12
Should the end positions be 26 and 11?