regex.c 793 B

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