当使用glib的g_regex_split_simple时,检查捕获组是否实际捕获了任何内容的最佳方法是什么?
#include <glib.h>
#include <stdio.h>
int main() {
char *teststring = "<initiator>{address == 10.20.30.40, port == 5060, transport == UDP}</initiator>";
char **addressMatches = g_regex_split_simple("address\\s*=*\\s*(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})", teststring, 0, 0);
// What is the proper way to test here that there was a match found?
// This sort of works sometimes, but can result in segfault on some platforms if a match is not found
if (addressMatches[2]) {
printf("IP: %s\n", addressMatches[1]);
}
}
1条答案
按热度按时间eyh26e7m1#
这似乎工作。