fork(1) download
  1. #include <sys/types.h>
  2. #include <regex.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6.  
  7. void match_all(regex_t *p, char *sz) {
  8. regmatch_t whole_match;
  9. int match = 0;
  10. size_t offset = 0;
  11. size_t length = strlen(sz);
  12. char result[BUFSIZ];
  13. int len;
  14.  
  15. while (regexec(p, sz + offset, 1, &whole_match, 0) == 0) {
  16. match = 1;
  17. len = whole_match.rm_eo - whole_match.rm_so;
  18. memcpy(result, sz + whole_match.rm_so, len);
  19. result[len] = 0;
  20. printf("Match: %s\n", result);
  21.  
  22. offset += whole_match.rm_eo + 1; // increase the starting offset
  23. if (offset > length) {
  24. break;
  25. }
  26. }
  27. if (! match) {
  28. printf("\"%s\" does not contain a match\n", sz);
  29. }
  30. }
  31.  
  32.  
  33. int main(int argc, char* argv[]) {
  34. int r;
  35. regex_t p;
  36. r = regcomp(&p, "[[:alnum:]]*k[[:alnum:]]*", 0);
  37. if (r != 0) {
  38. printf("regcomp failed\n");
  39. }
  40. match_all(&p, "mikko mikko");
  41. regfree(&p);
  42. return 0;
  43. }
Success #stdin #stdout 0s 5556KB
stdin
Standard input is empty
stdout
Match: mikko
Match: mikko