regex.c 833 B

1234567891011121314151617181920212223242526272829303132
  1. #include <regex.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main(void) {
  5. regex_t regex;
  6. char error_buf[256];
  7. int error = regcomp(&regex, "h.llo \\(w.rld\\)", REG_ICASE);
  8. if (error) {
  9. regerror(error, &regex, error_buf, 255);
  10. error_buf[255] = 0;
  11. printf("regcomp error: %d = %s\n", error, error_buf);
  12. return EXIT_FAILURE;
  13. }
  14. regmatch_t matches[3] = {{0}};
  15. error = regexec(&regex, "Hey, how are you? Hello? Hallo Wurld??", 3, matches, 0);
  16. regfree(&regex);
  17. if (error) {
  18. regerror(error, &regex, error_buf, 255);
  19. printf("regexec error: %d = %s\n", error, error_buf);
  20. return EXIT_FAILURE;
  21. }
  22. for (int group = 0; group < 3; group += 1) {
  23. printf("Matching group: %d - %d\n", matches[group].rm_so, matches[group].rm_eo);
  24. }
  25. }