fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <regex.h>
  4.  
  5. int main ()
  6. {
  7. char * source = "* * * * 00 00 00 ; reboot";
  8. char *regexString = "^(\\*|[[:digit:]]{1,9})[[:blank:]](\\*|[[:digit:]]{2})[[:blank:]](\\*|[[:digit:]]{2})[[:blank:]](\\*|[[:alpha:]]{3})[[:blank:]](\\*|[[:digit:]]{2})[[:blank:]](\\*|[[:digit:]]{2})[[:blank:]](\\*|[[:digit:]]{2})[[:blank:]];[[:blank:]]([[:print:]]*)";
  9. size_t maxMatches = 3; //I've tried for sevrals values, 2, 3 ... same Output
  10. size_t maxGroups = 9; // 8 groups + 1 for whole match
  11.  
  12. regex_t regexCompiled;
  13. regmatch_t groupArray[maxGroups];
  14. unsigned int m;
  15. char * cursor;
  16.  
  17. if (regcomp(&regexCompiled, regexString, REG_EXTENDED))
  18. {
  19. printf("Could not compile regular expression.\n");
  20. return 1;
  21. };
  22.  
  23. m = 0;
  24. cursor = source;
  25. for (m = 0; m < maxMatches; m ++)
  26. {
  27. if (regexec(&regexCompiled, cursor, maxGroups, groupArray, 0))
  28. break; // No more matches
  29.  
  30. unsigned int g = 0;
  31. unsigned int offset = 0;
  32. for (g = 0; g < maxGroups; g++)
  33. {
  34. if (groupArray[g].rm_so == (size_t)-1)
  35. break; // No more groups
  36.  
  37. if (g == 0)
  38. offset = groupArray[g].rm_eo;
  39.  
  40. char cursorCopy[strlen(cursor) + 1];
  41. strcpy(cursorCopy, cursor);
  42. cursorCopy[groupArray[g].rm_eo] = 0;
  43. printf("Match %u, Group %u: [%2u-%2u]: %s\n",
  44. m, g, groupArray[g].rm_so, groupArray[g].rm_eo,
  45. cursorCopy + groupArray[g].rm_so);
  46. }
  47. cursor += offset;
  48. }
  49.  
  50. regfree(&regexCompiled);
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 4520KB
stdin
Standard input is empty
stdout
Match 0, Group 0: [ 0-25]: * * * * 00 00 00 ; reboot
Match 0, Group 1: [ 0- 1]: *
Match 0, Group 2: [ 2- 3]: *
Match 0, Group 3: [ 4- 5]: *
Match 0, Group 4: [ 6- 7]: *
Match 0, Group 5: [ 8-10]: 00
Match 0, Group 6: [11-13]: 00
Match 0, Group 7: [14-16]: 00
Match 0, Group 8: [19-25]: reboot