home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / i18nv104.zip / SAMPLE / REGEX / REGEX.C < prev    next >
C/C++ Source or Header  |  1996-02-13  |  6KB  |  171 lines

  1. #include <wchar.h>
  2. #include <locale.h>
  3. #include <regex.h>
  4.  
  5. #define MAX_STR_SIZE   256
  6.  
  7. main(void) 
  8. {
  9.     regex_t    reg;            /* regular expression status buffer     */
  10.     int status,                /* regexec status                       */
  11.         error = 0;             /* error flag                           */             
  12.     char pattern[MAX_STR_SIZE],  /* pattern to look for            */
  13.          test_string[MAX_STR_SIZE];        /* test string          */        
  14.  
  15.     printf("%s\n", setlocale(LC_ALL, ""));
  16.  
  17.     strcpy(pattern,"m");
  18.     strcpy(test_string,"this is my test string");
  19.  
  20.     if(error = regcomp(®, pattern, REG_EXTENDED)) {
  21.         printf("error: %d\n", error);
  22.     }
  23.     else {
  24.         status = regexec(®, test_string, 0, NULL, 0);
  25.         if (status == 0)
  26.             printf("MATCH! : %s <- pattern found in -> %s\n", pattern, 
  27.                             test_string);
  28.         else
  29.             printf("NO MATCH! : %s <- pattern not found in -> %s\n", 
  30.                             pattern, test_string);
  31.     }
  32.     regfree(®);
  33.  
  34.     strcpy(pattern,"^match this$");
  35.     strcpy(test_string,"match this");
  36.  
  37.     if(error = regcomp(®, pattern, REG_EXTENDED)) {
  38.         printf("error: %d\n", error);
  39.     }
  40.     else {
  41.         status = regexec(®, test_string, 0, NULL, 0);
  42.         if (status == 0)
  43.             printf("MATCH! : %s <- pattern found in -> %s\n", 
  44.                    pattern, test_string);
  45.         else
  46.             printf("NO MATCH! : %s <- pattern not found in -> %s\n", 
  47.                    pattern, test_string);
  48.     }
  49.     regfree(®);
  50.  
  51.     strcpy(pattern,"match this");
  52.     strcpy(test_string,"try to match this");
  53.  
  54.     if(error = regcomp(®, pattern, REG_EXTENDED)) {
  55.         printf("error: %d\n", error);
  56.     }
  57.     else {
  58.         status = regexec(®, test_string, 0, NULL, 0);
  59.         if (status == 0)
  60.             printf("MATCH! : %s <- pattern found in -> %s\n", 
  61.                    pattern, test_string);
  62.         else
  63.             printf("NO MATCH! : %s <- pattern not found in -> %s\n", 
  64.                    pattern, test_string);
  65.     }
  66.     regfree(®);
  67.  
  68.     strcpy(pattern,"no match here");
  69.     strcpy(test_string,"bdfgijklpqsuvwxyz");
  70.  
  71.     if(error = regcomp(®, pattern, REG_EXTENDED)) {
  72.         printf("error: %d\n", error);
  73.     }
  74.     else {
  75.         status = regexec(®, test_string, 0, NULL, 0);
  76.         if (status == 0)
  77.             printf("MATCH! : %s <- pattern found in -> %s\n", 
  78.                    pattern, test_string);
  79.         else
  80.             printf("NO MATCH! : %s <- pattern not found in -> %s\n", 
  81.                    pattern, test_string);
  82.     }
  83.     regfree(®);
  84.  
  85.     strcpy(pattern,"[[:alpha:]]");
  86.     strcpy(test_string,"123456789012345678901234567890");
  87.  
  88.     if(error = regcomp(®, pattern, REG_EXTENDED)) {
  89.         printf("error: %d\n", error);
  90.     }
  91.     else {
  92.         status = regexec(®, test_string, 0, NULL, 0);
  93.         if (status == 0)
  94.             printf("MATCH! : %s <- pattern found in -> %s\n", 
  95.                    pattern, test_string);
  96.         else
  97.             printf("NO MATCH! : %s <- pattern not found in -> %s\n", 
  98.                    pattern, test_string);
  99.     }
  100.     regfree(®);
  101.  
  102.     strcpy(pattern,"[[:digit:]]");
  103.     strcpy(test_string,"123456789012345678901234567890");
  104.  
  105.     if(error = regcomp(®, pattern, REG_EXTENDED)) {
  106.         printf("error: %d\n", error);
  107.     }
  108.     else {
  109.         status = regexec(®, test_string, 0, NULL, 0);
  110.         if (status == 0)
  111.             printf("MATCH! : %s <- pattern found in -> %s\n", 
  112.                    pattern, test_string);
  113.         else
  114.             printf("NO MATCH! : %s <- pattern not found in -> %s\n", 
  115.                    pattern, test_string);
  116.     }
  117.     regfree(®);
  118.  
  119.     strcpy(pattern,"[a-d]");
  120.     strcpy(test_string,"this is my test 123 string.");
  121.  
  122.     if(error = regcomp(®, pattern, REG_EXTENDED)) {
  123.         printf("error: %d\n", error);
  124.     }
  125.     else {
  126.         status = regexec(®, test_string, 0, NULL, 0);
  127.         if (status == 0)
  128.             printf("MATCH! : %s <- pattern found in -> %s\n", 
  129.                    pattern, test_string);
  130.         else
  131.             printf("NO MATCH! : %s <- pattern not found in -> %s\n", 
  132.                    pattern, test_string);
  133.     }
  134.     regfree(®);
  135.  
  136.     strcpy(pattern,"[a-e]");
  137.     strcpy(test_string,"this is my test 123 string.");
  138.  
  139.     if(error = regcomp(®, pattern, REG_EXTENDED)) {
  140.         printf("error: %d\n", error);
  141.     }
  142.     else {
  143.         status = regexec(®, test_string, 0, NULL, 0);
  144.         if (status == 0)
  145.             printf("MATCH! : %s <- pattern found in -> %s\n", 
  146.                    pattern, test_string);
  147.         else
  148.             printf("NO MATCH! : %s <- pattern not found in -> %s\n", 
  149.                    pattern, test_string);
  150.     }
  151.     regfree(®);
  152.  
  153.     strcpy(pattern,"[^x]");
  154.     strcpy(test_string,"you won't find one letter like that here");
  155.  
  156.     if(error = regcomp(®, pattern, REG_EXTENDED)) {
  157.         printf("error: %d\n", error);
  158.     }
  159.     else {
  160.         status = regexec(®, test_string, 0, NULL, 0);
  161.         if (status == 0)
  162.             printf("MATCH! : %s <- pattern found in -> %s\n", 
  163.                    pattern, test_string);
  164.         else
  165.             printf("NO MATCH! : %s <- pattern not found in -> %s\n", 
  166.                    pattern, test_string);
  167.     }
  168.     regfree(®);
  169.  
  170. }
  171.