fork(2) download
  1. #include <stdio.h>
  2. #include <regex.h>
  3.  
  4. int main(void) {
  5.  
  6.  
  7. const char *regular_expression_pattern_keyword = "^(auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while)";
  8. const char *regular_expression_pattern_identifier = "^[[:alpha:]]{1,}";
  9. const char *regular_expression_pattern_number = "^[[:digit:]]+(\\W|$)";
  10. const char *regular_expression_pattern_punctuator = "^[[:punct:]]";
  11.  
  12. const char *patterns[] = {
  13. regular_expression_pattern_keyword,
  14. regular_expression_pattern_identifier,
  15. regular_expression_pattern_number,
  16. regular_expression_pattern_punctuator
  17. };
  18.  
  19. for (int i = 0; i < 4; i++)
  20. {
  21. regex_t regular_expression;
  22. int status;
  23.  
  24. status = regcomp(&regular_expression, patterns[i], REG_EXTENDED);
  25.  
  26. if (status)
  27. {
  28. // FIXME: Improve error handling!
  29. printf("Error: Failed to compile regex!\n");
  30. exit(1);
  31. }
  32.  
  33. status = regexec(&regular_expression, "12 abc", 0, NULL, 0);
  34.  
  35. if (!status)
  36. {
  37. printf("Regex status: Match ->%s\n", patterns[i]);
  38. }
  39.  
  40. else if (status == REG_NOMATCH)
  41. {
  42. printf("Regex status: No match\n");
  43. }
  44.  
  45. else
  46. {
  47. // FIXME: Improve error handling!
  48. printf("Error: Failed to match regex!\n");
  49. exit(1);
  50. }
  51.  
  52. regfree(&regular_expression);
  53. }
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
Regex status: No match
Regex status: No match
Regex status: Match ->^[[:digit:]]+(\W|$)
Regex status: No match