fork download
  1. #include <sys/types.h>
  2. #include <regex.h>
  3. #include <stdio.h>
  4.  
  5. int main(int argc, char *argv[]){
  6. regex_t regex;
  7. int reti;
  8. char msgbuf[100];
  9. char inputStr2[100]="12:34:04";
  10. char inputStr[100]="12:34";
  11.  
  12. /* Compile regular expression */
  13. reti = regcomp(&regex,"^\\(\\(\\([01]?[0-9]|2[0-3]\\):\\)?\\([0-5]?[0-9]\\):\\)?\\([0-5]?[0-9]\\)$"
  14. , 0);
  15. if( reti ){
  16. fprintf(stderr, "Could not compile regex\n");
  17. }
  18.  
  19. /* Execute regular expression */
  20. printf("%s is the string\n",inputStr);
  21. reti = regexec(&regex, inputStr, 0, NULL, 0);
  22. if( !reti ){
  23. puts("Match");
  24. }
  25. else if( reti == REG_NOMATCH ){
  26. puts("No match");
  27. }
  28. else{
  29. regerror(reti, &regex, msgbuf, sizeof(msgbuf));
  30. fprintf(stderr, "Regex match failed: %s\n", msgbuf);
  31. }
  32. printf("%s is the string\n",inputStr2);
  33. reti = regexec(&regex, inputStr2, 0, NULL, 0);
  34. if( !reti ){
  35. puts("Match");
  36. }
  37. else if( reti == REG_NOMATCH ){
  38. puts("No match");
  39. }
  40. else{
  41. regerror(reti, &regex, msgbuf, sizeof(msgbuf));
  42. fprintf(stderr, "Regex match failed: %s\n", msgbuf);
  43. }
  44. /* Free compiled regular expression if you want to use the regex_t again */
  45. regfree(&regex);
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
12:34 is the string
No match
12:34:04 is the string
No match