try-re.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. int regmatch( regex_t *preg, const char * string, size_t nmatch, regmatch_t pmatch[], int eflags) {
  9. // returns number of matches found. (Max nmatch)
  10. int matches = 0;
  11. int offset = 0;
  12. int ret;
  13. while (matches < nmatch) {
  14. ret = regexec( preg, string + offset, nmatch - matches, pmatch + matches, eflags);
  15. if (!ret) {
  16. int current = offset;
  17. offset += pmatch[matches].rm_eo;
  18. pmatch[matches].rm_so += current;
  19. pmatch[matches].rm_eo += current;
  20. matches++;
  21. } else if (ret == REG_NOMATCH ) {
  22. break;
  23. } else {
  24. break;
  25. }
  26. }
  27. return matches;
  28. }
  29. void test(const char * trythis) {
  30. regmatch_t regmatches[MAX_MATCH];
  31. int matches, x;
  32. printf("TEST (%s)\n", trythis);
  33. matches = regmatch(&regex, trythis, MAX_MATCH, regmatches, 0);
  34. if (matches == 0) {
  35. printf("No matches.\n");
  36. } else {
  37. printf("%d matches:\n", matches);
  38. for( x = 0; x < matches; x++) {
  39. printf("%d (%d - %d)\n", x, regmatches[x].rm_so, regmatches[x].rm_eo);
  40. printf("[%.*s]\n", regmatches[x].rm_eo - regmatches[x].rm_so, trythis + regmatches[x].rm_so);
  41. }
  42. }
  43. }
  44. void test_( const char * trythis) {
  45. int ret;
  46. char msgbuf[100];
  47. const char * p = trythis;
  48. // safe printing (maybe)
  49. strcpy(msgbuf, trythis);
  50. char * fixup = msgbuf;
  51. while ( ( fixup = strstr(fixup, "\x1b") ) != NULL ) {
  52. *fixup = '^';
  53. };
  54. printf("Test: [%s]%s\n", msgbuf, RESET);
  55. regmatch_t matches[ MAX_MATCH ];
  56. while (1) {
  57. ret = regexec( &regex, p, MAX_MATCH, matches, 0 );
  58. if (!ret) {
  59. printf("MATCH!\n");
  60. for (int i = 0; i < MAX_MATCH; i++) {
  61. int start, finish;
  62. if (matches[i].rm_so == -1 )
  63. break;
  64. start = matches[i].rm_so;
  65. finish = matches[i].rm_eo;
  66. // %.*s = length to print, char * to use
  67. strncpy(msgbuf, p+start, (finish-start));
  68. msgbuf[finish-start] = 0;
  69. fixup = msgbuf;
  70. while ( ( fixup = strstr(fixup, "\x1b") ) != NULL ) {
  71. *fixup = '^';
  72. };
  73. // printf("'%.*s'%s %d : %d - %d\n", (finish - start), p + start, RESET, i, start, finish);
  74. printf("'%s' %d : %d - %d\n", msgbuf, i, start, finish);
  75. };
  76. p += matches[0].rm_eo;
  77. }
  78. else if (ret == REG_NOMATCH) {
  79. printf("Sorry, no matches.\n");
  80. break;
  81. }
  82. else {
  83. regerror( ret, &regex, msgbuf, sizeof(msgbuf));
  84. fprintf(stderr, "Regex match failed: %s\n", msgbuf);
  85. }
  86. }
  87. return;
  88. }
  89. int main(int argc, char * argv[]) {
  90. // char RX[] = "(\x1b\[(?:\\d+|;)+[a-zA-Z])";
  91. //char RX[] = "([a-z][a-z]([0-9]+))";
  92. char RX[] = "([a-z][a-z]([0-9]+))";
  93. char msgbuf[100];
  94. int ret;
  95. if ( ret = regcomp( &regex, RX, REG_EXTENDED|REG_NEWLINE ) ) {
  96. regerror( ret, &regex, msgbuf, sizeof(msgbuf));
  97. fprintf(stderr, "Regex compile failed: %s\n", msgbuf);
  98. return 1;
  99. }
  100. test("this will fail.");
  101. test("this has ab5 ab55 zd3 three matches.");
  102. regfree(&regex);
  103. // if ( regcomp( &regex, "(\x1b\[(?:[0-9]|;)+[a-zA-Z])", REG_EXTENDED|REG_NEWLINE ) ) {
  104. // if ( regcomp( &regex, "(\x1b\[([0-9]+|;)+[a-zA-Z])", REG_EXTENDED|REG_NEWLINE ) ) {
  105. // if ( regcomp( &regex, "\x1b\[[0-9]+(;[0-9]+)+?[a-zA-Z]", REG_EXTENDED|REG_NEWLINE ) ) {
  106. if ( regcomp( &regex, "\x1b\[[0-9]+(;[0-9]+)*?[a-zA-Z]", REG_EXTENDED|REG_NEWLINE ) ) {
  107. regerror( ret, &regex, msgbuf, sizeof(msgbuf));
  108. fprintf(stderr, "Regex compile failed: %s\n", msgbuf);
  109. return 1;
  110. };
  111. test("\x1b[1;1H\x1b[2J\x0cMystic BBS\x1b[6n");
  112. test("\x1b[5;1HMEEP.");
  113. test("\x1b[0;1;34;47mHello");
  114. regfree(&regex);
  115. // \s matches space, FORM FEED. Ouch! NO!
  116. if ( regcomp( &regex, "[a-zA-Z]+( [a-zA-Z]+)+", REG_EXTENDED|REG_NEWLINE ) ) {
  117. regerror( ret, &regex, msgbuf, sizeof(msgbuf));
  118. fprintf(stderr, "Regex compile failed: %s\n", msgbuf);
  119. return 1;
  120. };
  121. test("\x1b[1;1H\x1b[2J\x0cMystic BBS\x1b[6n");
  122. test("\x1b[5;1HMEEP is cool for cats.");
  123. regfree(&regex);
  124. return 0;
  125. }