match
Description:
Scans for a match in string
for the pattern in this.
The match_options
are combined with the match options specified when the this structure was
created, letting you have more flexibility in reusing Regex structures.
Unless g_regex_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 null. Note that if match_info
is not null
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 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 g_match_info_free
) after freeing or modifying
string
then the behaviour is undefined.
Example: Regex, match:
public int main (string[] args){
// Output: ``s[ai]mple matched!``
try {
string quote = "This is a simple sample.";
Regex regex = new Regex ("s[ai]mple");
if (regex.match (quote)){
print ("%s matched!\n", regex.get_pattern ());
}
} catch (RegexError e) {
print ("Error %s\n", e.message);
}
return 0;
}
valac --pkg glib-2.0 GLib.Regex.match.vala
Parameters:
this | |
match_options |
match options |
match_info |
pointer to location where to store the MatchInfo, or null if you do not need it |
string |
the string to scan for matches |
Returns:
true is the string matched, false otherwise |