fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <regex.h>
  4.  
  5.  
  6. static const char *char_match = "^('(.|([\\]0))')";
  7.  
  8. int main()
  9. {
  10. regex_t re = {0};
  11. if (regcomp(&re, char_match, REG_EXTENDED) != 0)
  12. {
  13. perror("Failed to compile RE.");
  14. exit(EXIT_FAILURE);
  15. }
  16.  
  17. // expected successed
  18. const char *good[] =
  19. {
  20. "'a'",
  21. "'\\0'",
  22. "'0'",
  23. "'\\'"
  24. };
  25. for (int i=0;i<sizeof(good)/sizeof(good[0]);++i)
  26. {
  27. int match = regexec(&re, good[i], 0, NULL, 0);
  28. printf("good:%d\n", match);
  29. }
  30.  
  31. const char *bad[] =
  32. {
  33. "'ab'",
  34. "'a",
  35. "\\",
  36. "\\\0",
  37. "\0\\\0"
  38. };
  39. for (int i=0;i<sizeof(bad)/sizeof(bad[0]);++i)
  40. {
  41. int match = regexec(&re, bad[i], 0, NULL, 0);
  42. printf("bad:%d\n", match);
  43. }
  44.  
  45. regfree(&re);
  46. return 0;
  47. }
Success #stdin #stdout 0s 1964KB
stdin
Standard input is empty
stdout
good:0
good:0
good:0
good:0
bad:1
bad:1
bad:1
bad:1
bad:1