Email: bknpk@hotmail.com Phone: +972-54-7649119


V

 

Simple c like regular expression. The regular expression is stored in a buffer, compiled and then used.
More examples of regular expression as well as other cpp examples are at: main page. Some of these cpp regular expression use boost library. I was not able to install the boost on my debian wheezy. For this example I need no installation for any package, to get it work well, on a lenny debian machine.

  1. #include <sys/types.h>
  2. #include <regex.h>
  3. #include <locale.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <nl_types.h>
  7. #define SLENGTH 128
  8. int match(char *pattern, char *string)
  9. {
  10.   char message[SLENGTH];
  11.   char *start_search;
  12.   int error, msize, count;
  13.   regex_t preg;
  14.   regmatch_t pmatch;
  15.   
  16.   //REG_ICASE | REG_EXTENDED);
  17.   error = regcomp(&preg, pattern, REG_EXTENDED);
  18.   if (error) {
  19.     msize = regerror(error, &preg, message, SLENGTH);
  20.     printf("%s\n", message);
  21.     if (msize > SLENGTH)
  22.     printf("Additional text lost\n");
  23.     return 1;
  24.   }
  25.   error = regexec(&preg, string, 1, &pmatch, 0);
  26.   if (error == REG_NOMATCH) {
  27.     printf("No matches in string\n");
  28.     return 1;
  29.   } else if (error != 0) {
  30.     msize = regerror(error, &preg, message, SLENGTH);
  31.     printf("%s\n", message);
  32.     if (msize > SLENGTH)
  33.     printf("Additional text lost\n");
  34.     return 1;
  35.   };
  36.   count = 1;
  37.   start_search = string + pmatch.rm_eo;
  38.   while (error == 0) {
  39.     error =
  40.     regexec(&preg, start_search, 1, &pmatch, REG_NOTBOL);
  41.     start_search = start_search + pmatch.rm_eo;
  42.     count++;
  43.     printf("match at %d\n", pmatch.rm_eo);
  44.     //Print sub match
  45.     unsigned matchlen;
  46.     matchlen=pmatch.rm_eo - pmatch.rm_so;
  47.     char * submatch = new char[matchlen+1];
  48.     strncpy(submatch, &string[pmatch.rm_so], matchlen+1);
  49.     submatch[matchlen]='\0';
  50.     printf("%s\n", submatch);
  51.     delete[] submatch;
  52.   };
  53.   count--;
  54.   printf("There were %d matches\n", count);
  55.   regfree(&preg);
  56. }
  57. main()
  58. {
  59.   char patt[SLENGTH], strng[SLENGTH];
  60.   char *eol;
  61.   
  62.   (void)setlocale(LC_ALL, "");
  63.   
  64.   printf("Enter a regular expression:");
  65.   fgets(patt, SLENGTH, stdin);
  66.   if ((eol = strchr(patt, '\n')) != NULL)
  67.   *eol = '\0'; /* Replace newline with null */
  68.   else
  69.   return 1; /* Line entered too long */
  70.   printf("Enter string to compare\nString: ");
  71.   fgets(strng, SLENGTH, stdin);
  72.   if ((eol = strchr(strng, '\n')) != NULL)
  73.   *eol = '\0'; /* Replace newline with null */
  74.   else return 1; /* Line entered too long */
  75.   
  76.   match(patt, strng);
  77. }

  ...


Search This Site


Feedback This Site





new pages on this site