From Scribby Shama, 4 Years ago, written in C.
This paste is a reply to Re: Untitled from Corrupt Pintail
- view diff
Embed
  1. #define _GNU_SOURCE
  2.  
  3. #include <regex.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. // http://stackoverflow.com/questions/2359811/working-with-gnu-regex-functions-in-c-or-c
  8.  
  9. int main() {
  10.    int i;
  11.    //char pat_str[] = "\\([^#]*\\)\\(FS#[0-9]+\\).*";
  12.    char pat_str[] = "([^#]*)(FS#[0-9]+).*";
  13.    char pat_str2[] = "FS#[0-9]+";
  14.    //char pat_str[] = ".*(FS#[0-9]+).*";
  15.    struct re_pattern_buffer pat_buff;
  16.    struct re_pattern_buffer pat_buff2;
  17.    struct re_registers regs;
  18.    pat_buff.translate = 0;
  19.    pat_buff.fastmap = 0;
  20.    pat_buff.buffer = 0;
  21.    pat_buff.allocated = 0;
  22.    pat_buff.no_sub = 0;
  23.    pat_buff2.translate = 0;
  24.    pat_buff2.fastmap = 0;
  25.    pat_buff2.buffer = 0;
  26.    pat_buff2.allocated = 0;
  27.    pat_buff2.no_sub = 0;
  28.    re_syntax_options = RE_DOT_NEWLINE | RE_NO_BK_PARENS;
  29.  
  30.    re_compile_pattern(pat_str, sizeof(pat_str) - 1, &pat_buff);
  31.    re_compile_pattern(pat_str2, sizeof(pat_str2) - 1, &pat_buff2);
  32.    char* strings[] = {"Partially implements FS#6: Conference ID changed to c+bugs@conference.thedevstack.de", "abc\nblubb FS#123 abc", "FS#123 abcdef", "abcdef FS#123", "blubber", "FS#123 abc FS#234 def ghi FS#345 jkl FS#456"};
  33.  
  34.    for (i = 0; i < sizeof(strings)/sizeof(char*); i++) {
  35.       printf("Text:\n>>>>>>>>>>>>>>>>>>>>\n%s\n<<<<<<<<<<<<<<<<<<<<\n\n", strings[i]);
  36.       int match_ret = 0;
  37.       int last_match = 0;
  38.       for (;;) {
  39.          match_ret = re_match(&pat_buff, strings[i], strlen(strings[i]), last_match, &regs);
  40.          if (match_ret >= 0) {
  41.             int j;
  42.             // first is all, last is -1,-1
  43.             for (j = 1; j < regs.num_regs - 1; j++) {
  44.                int s = regs.start[j];
  45.                int e = regs.end[j];
  46.                if ((e - s) > 0) {
  47.                   printf("\"%.*s\"", e - s, strings[i] + s);
  48.                   if (re_match(&pat_buff2, strings[i] + s, strlen(strings[i] + s), 0, NULL) > 0)
  49.                      printf(" <<< found");
  50.                   printf("\n");
  51.                }
  52.             }
  53.             last_match = regs.end[regs.num_regs - 2];
  54.          } else if (match_ret == -1) {
  55.             if (strlen(strings[i]+last_match))
  56.                printf("\"%s\"\n", strings[i] + last_match);
  57.             break;
  58.          } else {
  59.             printf("error\n");
  60.             break;
  61.          }
  62.       }
  63.       printf("\n\n");
  64.    }
  65.  
  66.    regfree(&pat_buff);
  67.    regfree(&pat_buff2);
  68. }
  69.  
  70.