home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Examples / MiscStringRegex / test.m < prev   
Encoding:
Text File  |  1994-03-15  |  2.2 KB  |  69 lines

  1. #import <misckit/MiscString.h>
  2.  
  3. #define NUMREG 11
  4. #define NUMSTR 2
  5. void main() {
  6.  
  7.  id ts2, tstr = [MiscString newWithString:" 678HelLo My dear 44bonnie.3"];
  8.  int i,j,spot,len,num,k;
  9.  id a = [MiscString new],b= [MiscString new],c= [MiscString new];
  10.  int numreg = NUMREG;
  11.  char *regarr[NUMREG] =  {"[0-9]",
  12.                           "[0-9]*",
  13.                   "([0-9])+",
  14.                   "[^0-9]+",
  15.                   "[a-z]+",
  16.               "[a-zA-Z]+",
  17.               "[{][^}]*[}]",
  18.               NULL,
  19.               "[[0-9]**",
  20.               "[0-9]?[a-zA-Z]+",
  21.               "([^0-9]+)"};
  22.  
  23. char *strarr[NUMSTR] = {" 678HelLo My dear 44bonnie.3",
  24.                         "678HelLo My de(**)ar 44bo{}nnie.3"};
  25.  
  26. for (k=0;k<NUMSTR;k++) {
  27. [tstr setStringValue:strarr[k]];
  28.  
  29. for (i=0;i<numreg;i++) {
  30.   printf("String is : \"%s\"\n",[tstr stringValue]);
  31.   printf("numOf '%s'       : %d\n",regarr[i],num = [tstr numOfRegex:regarr[i] caseSensitive:YES]);
  32.   printf("numOf '%s' nocase: %d\n",regarr[i],[tstr numOfRegex:regarr[i] caseSensitive:NO]);
  33.  
  34.   printf("(-1 is MISC_STRING_LAST)\n");
  35.   for (j=-1;j<=num;j++) {
  36.    spot = [tstr grep:regarr[i] occurrenceNum:j caseSensitive:YES before:a middle:b after:c];
  37.    printf("%d: retval: %d  before: '%s'  middle: '%s'  after: '%s'\n",j,spot,
  38.       [a stringValue],
  39.       [b stringValue],
  40.       [c stringValue]);
  41.    spot = [tstr spotOfRegex:regarr[i] occurrenceNum:j caseSensitive:YES length:&len];
  42.    printf("spot: %d  len: %d\n",spot,len);
  43.    spot = [tstr rspotOfRegex:regarr[i] occurrenceNum:j caseSensitive:YES length:&len];
  44.    printf("rspot: %d  len: %d\n",spot,len);
  45.   } 
  46.  
  47.  ts2 = [tstr copy];
  48.  spot = [ts2 replaceEveryOccurrenceOfRegex:regarr[i] with:"[]" caseSensitive:YES];
  49.  printf("After replace all '%s' with '[]': '%s'\n",regarr[i],[ts2 stringValue]);
  50.  printf("(%d replacement(s))\n",spot);
  51.  [ts2 free];
  52.  
  53.  for (j=-1;j<=num+1;j++) {
  54.    ts2 = [tstr copy];
  55.    [ts2 replaceRegex:regarr[i] with:"[]" occurrenceNum:j caseSensitive:YES];
  56.    printf("replace %dth '%s' with '[]':  '%s'\n",j,regarr[i],[ts2 stringValue]);
  57.    [ts2 free];
  58.   }
  59.  
  60.  printf("Matches '%s'? %d\n",regarr[i],[tstr matchesRegex:regarr[i]]);
  61.  printf("----------------------------------------------------------\n");
  62. }
  63. }
  64.  [tstr free];
  65.  [a free];
  66.  [b free];
  67.  [c free];
  68.  
  69.  }