#include #include #include #include int main() { const char* s = "sip:012345678@11.111.11.111:5060;user=phone"; const char* re = "^\\([[:alpha:]][[:alnum:]]\\+:\\)\\?/\\?/\\?\\([^[:space:][:cntrl:]@]\\+@\\)\\?\\([[:alnum:]._+-]\\+\\|[[][[:xdigit:].:]\\+[]]\\)\\(:[0-9]\\+\\)\\?"; regex_t* data = (regex_t*)malloc(sizeof(regex_t)); regcomp(data, re, 0); const int MAX_MATCH = 9; regmatch_t rmatch[MAX_MATCH]; regexec(data, s, MAX_MATCH, rmatch, 0); for (int i = 0; i < MAX_MATCH; i++) { char substr[256]; unsigned substr_len = rmatch[i].rm_eo - rmatch[i].rm_so; memcpy(substr, s + rmatch[i].rm_so, substr_len); substr[substr_len] = '\0'; printf("Match %u: %2d - %2d \t%s\n", i, rmatch[i].rm_so, rmatch[i].rm_eo, substr_len > 0? substr : ""); } return 0; } /* glibc: Match 0: 0 - 32 sip:012345678@11.111.11.111:5060 Match 1: 0 - 4 sip: Match 2: 4 - 14 012345678@ Match 3: 14 - 27 11.111.11.111 Match 4: 27 - 32 :5060 Match 5: -1 - -1 Match 6: -1 - -1 Match 7: -1 - -1 Match 8: -1 - -1 musl 1.1.19: Match 0: 0 - 32 sip:012345678@11.111.11.111:5060 Match 1: -1 - -1 Match 2: 0 - 14 sip:012345678@ Match 3: 14 - 27 11.111.11.111 Match 4: 27 - 32 :5060 Match 5: -1 - -1 Match 6: -1 - -1 Match 7: -1 - -1 Match 8: -1 - -1 */