try-re.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <regex.h>
  4. regex_t regex;
  5. #define MAX_MATCH 5
  6. // This is the number of capture groups + 1
  7. #define RESET "\x1b[0m"
  8. void test( const char * trythis) {
  9. int ret;
  10. char msgbuf[100];
  11. const char * p = trythis;
  12. // safe printing (maybe)
  13. strcpy(msgbuf, trythis);
  14. char * fixup = msgbuf;
  15. while ( ( fixup = strstr(fixup, "\x1b") ) != NULL ) {
  16. *fixup = '^';
  17. };
  18. printf("Test: [%s]%s\n", msgbuf, RESET);
  19. regmatch_t matches[ MAX_MATCH ];
  20. while (1) {
  21. ret = regexec( &regex, p, MAX_MATCH, matches, 0 );
  22. if (!ret) {
  23. printf("MATCH!\n");
  24. for (int i = 0; i < MAX_MATCH; i++) {
  25. int start, finish;
  26. if (matches[i].rm_so == -1 )
  27. break;
  28. start = matches[i].rm_so;
  29. finish = matches[i].rm_eo;
  30. // %.*s = length to print, char * to use
  31. strncpy(msgbuf, p+start, (finish-start));
  32. msgbuf[finish-start] = 0;
  33. fixup = msgbuf;
  34. while ( ( fixup = strstr(fixup, "\x1b") ) != NULL ) {
  35. *fixup = '^';
  36. };
  37. // printf("'%.*s'%s %d : %d - %d\n", (finish - start), p + start, RESET, i, start, finish);
  38. printf("'%s' %d : %d - %d\n", msgbuf, i, start, finish);
  39. };
  40. p += matches[0].rm_eo;
  41. }
  42. else if (ret == REG_NOMATCH) {
  43. printf("Sorry, no matches.\n");
  44. break;
  45. }
  46. else {
  47. regerror( ret, &regex, msgbuf, sizeof(msgbuf));
  48. fprintf(stderr, "Regex match failed: %s\n", msgbuf);
  49. }
  50. }
  51. return;
  52. }
  53. int main(int argc, char * argv[]) {
  54. // char RX[] = "(\x1b\[(?:\\d+|;)+[a-zA-Z])";
  55. //char RX[] = "([a-z][a-z]([0-9]+))";
  56. char RX[] = "([a-z][a-z]([0-9]+))";
  57. char msgbuf[100];
  58. int ret;
  59. if ( ret = regcomp( &regex, RX, REG_EXTENDED|REG_NEWLINE ) ) {
  60. regerror( ret, &regex, msgbuf, sizeof(msgbuf));
  61. fprintf(stderr, "Regex compile failed: %s\n", msgbuf);
  62. return 1;
  63. }
  64. test("this will fail.");
  65. test("this has ab5 ab55 zd3 three matches.");
  66. regfree(&regex);
  67. // if ( regcomp( &regex, "(\x1b\[(?:[0-9]|;)+[a-zA-Z])", REG_EXTENDED|REG_NEWLINE ) ) {
  68. // if ( regcomp( &regex, "(\x1b\[([0-9]+|;)+[a-zA-Z])", REG_EXTENDED|REG_NEWLINE ) ) {
  69. // if ( regcomp( &regex, "\x1b\[[0-9]+(;[0-9]+)+?[a-zA-Z]", REG_EXTENDED|REG_NEWLINE ) ) {
  70. if ( regcomp( &regex, "\x1b\[[0-9]+(;[0-9]+)*?[a-zA-Z]", REG_EXTENDED|REG_NEWLINE ) ) {
  71. regerror( ret, &regex, msgbuf, sizeof(msgbuf));
  72. fprintf(stderr, "Regex compile failed: %s\n", msgbuf);
  73. return 1;
  74. };
  75. test("\x1b[1;1H\x1b[2J\x0cMystic BBS\x1b[6n");
  76. test("\x1b[5;1HMEEP.");
  77. test("\x1b[0;1;34;47mHello");
  78. regfree(&regex);
  79. // \s matches space, FORM FEED. Ouch! NO!
  80. if ( regcomp( &regex, "[a-zA-Z]+( [a-zA-Z]+)+", REG_EXTENDED|REG_NEWLINE ) ) {
  81. regerror( ret, &regex, msgbuf, sizeof(msgbuf));
  82. fprintf(stderr, "Regex compile failed: %s\n", msgbuf);
  83. return 1;
  84. };
  85. test("\x1b[1;1H\x1b[2J\x0cMystic BBS\x1b[6n");
  86. test("\x1b[5;1HMEEP is cool for cats.");
  87. regfree(&regex);
  88. return 0;
  89. }