方法
GLibRegexmatch
自:2.14
声明 [src]
gboolean
g_regex_match (
const GRegex* regex,
const gchar* string,
GRegexMatchFlags match_options,
GMatchInfo** match_info
)
描述 [src]
在 string 中扫描与 regex 模式匹配的内容。将 match_options 与创建 regex 结构时指定的匹配选项组合,您可以在重用 GRegex 结构时拥有更多灵活性。
除非在选项中指定了 G_REGEX_RAW,否则 string 必须是有效的 UTF-8。
一个用于获取匹配信息的 GMatchInfo 结构体存储在 match_info 中(如果非 NULL)。注意,如果 match_info 非 NULL,则在函数返回 FALSE 时也会创建它,即即使正则表达式没有实际匹配,您也必须释放它。
要检索字符串中模式的全部非重叠匹配,您可以使用 g_match_info_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 不被复制并在内部使用于 GMatchInfo。如果在释放或修改 string 之后使用任何 GMatchInfo 方法(除 g_match_info_free() 外),则行为是未定义的。
自:2.14
参数
string-
类型:
const gchar*扫描匹配的字符串。
数据由方法调用者所有。 值是一个以 NUL 结尾的 UTF-8 字符串。 match_options-
类型:
GRegexMatchFlags匹配选项。
match_info-
类型:
GMatchInfo存储
GMatchInfo的位置的指针,或NULL如果不需要。该参数将由函数设置。 该参数可以是 NULL。方法调用者将获取返回数据的所有权,并负责释放它。