fork(2) download
  1. #include <stdio.h>
  2. #include <regex.h>
  3.  
  4. int match(const char *string, const char *pattern) {
  5. regex_t re;
  6.  
  7. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
  8. return 0;
  9. }
  10. int status = regexec(&re, string, 0, NULL, 0);
  11. regfree(&re);
  12. if (status != 0) {
  13. return 0;
  14. }
  15.  
  16. return 1;
  17. }
  18.  
  19. int main(void) {
  20. const char *reg = "^[a-zA-Z0-9_-]+\\.csv$";
  21. if (!match("test.csv", reg)) {
  22. printf("Not a valid csv file.\n");
  23. } else {
  24. printf("Valid csv file.\n");
  25. }
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
Valid csv file.