fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <regex.h>
  5.  
  6. typedef struct { char str[1000]; } regex;
  7.  
  8. long regexmatch(const char* str,
  9. const char* regexs,
  10. size_t nummatch,
  11. regex rp[nummatch]);
  12. void printres(regex reg[]);
  13.  
  14. int main(void) {
  15. regex* r=calloc(10, sizeof *r);
  16. if (regexmatch("T/2/b", "([A-Z]+)/([0-9]+)/([a-z]+)", 10, r)==0){
  17. printres(r);
  18. }
  19. free(r);
  20. return 0;
  21. }
  22.  
  23. long regexmatch(const char* str,
  24. const char* regexs,
  25. size_t nummatch,
  26. regex reg[nummatch]){
  27. regex_t r;regmatch_t match[nummatch];
  28. if (regcomp(&r,regexs,REG_EXTENDED) != 0){return -1;}
  29. if (regexec(&r,str,nummatch,match,0)!=0){regfree(&r);return -1;}
  30. regfree(&r);
  31. size_t i=0;
  32. for (i=0;i<nummatch;i++){
  33. if (match[i].rm_so > -1){
  34. unsigned long sz=match[i].rm_eo-match[i].rm_so;
  35. if (sz > 0 && sz < 1000){
  36. memcpy(reg->str, (char*)(str+match[i].rm_so), sz);
  37. reg++; /* This should be outside the if statement */
  38. }
  39. }
  40. }
  41. return 0;
  42. }
  43.  
  44. void printres(regex reg[]){
  45. printf("Matches\n");
  46. while (reg->str[0] != '\0'){
  47. printf("%s\n",reg->str);
  48. reg++;
  49. }
  50. }
Success #stdin #stdout 0s 2252KB
stdin
Standard input is empty
stdout
Matches
T/2/b
T
2
b