home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / red98104.zip / testre.cmd < prev   
OS/2 REXX Batch file  |  1998-04-14  |  5KB  |  149 lines

  1. /* Small Test program for my REXX Regular Expression Code */
  2.  
  3. /*--- Load up functions we will use -----------------------------------------*/
  4. call RxFuncAdd   'RegExpVersion', 'RxRegExp', 'RegExpVersion';
  5. call RxFuncAdd   'RegExpCompile', 'RxRegExp', 'RegExpCompile';
  6. call RxFuncAdd   'RegExpMatch',   'RxRegExp', 'RegExpMatch';
  7. call RxFuncAdd   'RegExpReplace', 'RxRegExp', 'RegExpReplace';
  8.  
  9. /*--- Make sure Regular Expression DLL is available for use -----------------*/
  10. say 'RxRegExp.DLL';
  11. say '~~~~~~~~~~~~';
  12. if RegExpOk() = 'N' then
  13. do
  14.    say "Regular Expressions can't be used (DLL probably unavailable)!";
  15.    exit(GetLineNumber());
  16. end;
  17. say "Available";
  18.  
  19. /*--- Display Version Info --------------------------------------------------*/
  20. say '';
  21. say 'RegExpVersion';
  22. say '~~~~~~~~~~~~~';
  23. VerRc = RegExpVersion('ReVersion');
  24. if VerRc <> 'OK' then
  25. do
  26.    say '    COULD NOT GET RXREGEXP.DLL VERSION INFO'
  27.    say '    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
  28.    say '    ' || VerRc;
  29.    exit(GetLineNumber());
  30. end;
  31. parse var ReVersion DllVersion WebPage EmailAddress AuthorName;
  32. say 'Version  = ' || DllVersion;
  33. say 'Author   = ' || AuthorName;
  34. say 'Email    = ' || EmailAddress;
  35. say 'Web Page = ' || WebPage;
  36.  
  37. /*--- Define/Compile regular expression -------------------------------------*/
  38. say ''
  39. say 'RegExpCompile';
  40. say '~~~~~~~~~~~~~';
  41. Re = arg(1);
  42. if Re = '' then
  43.    Re = "1[12]";
  44. CompileRc = RegExpCompile(Re);
  45. say CompileRc;
  46. if CompileRc <> 'OK' then
  47.    exit(GetLineNumber());
  48.  
  49. /*--- Look for a match ------------------------------------------------------*/
  50. LookIn = "AAA112BBB";
  51. say ''
  52. say 'RegExpMatch';
  53. say '~~~~~~~~~~~';
  54. say 'Looking for ' || Re || ' in "' || LookIn || '"';
  55. Answer  = '?';
  56. MatchRc = RegExpMatch(LookIn, "Answer");
  57. if MatchRc <> 'OK' then
  58. do
  59.    /*--- An error occurred --------------------------------------------------*/
  60.    say '    ERROR'
  61.    say '    ~~~~~'
  62.    say '    ' || MatchRc;
  63.    exit(GetLineNumber());
  64. end
  65. else
  66. do
  67.    /*--- Did we have any matches? -------------------------------------------*/
  68.    if  Answer = '' then
  69.    do
  70.        say '   * No Matches';
  71.        exit(GetLineNumber());
  72.    end;
  73.  
  74.    /*--- List all matches and submatches ------------------------------------*/
  75.    say '   * MatchList = ' || Answer;
  76.  
  77.    /*--- Extract the overall match information ------------------------------*/
  78.    parse var Answer MatchStart MatchLength SubMatch;
  79.    say '   * Match starts at posn ' || MatchStart || ' and is ' || MatchLength || ' bytes long.';
  80.  
  81.    /*--- Extract the submatch information -----------------------------------*/
  82.    Index = 0;
  83.    do  forever
  84.        /*--- Extract info ---------------------------------------------------*/
  85.        parse var SubMatch SubMatchStart SubMatchLength SubMatch;
  86.  
  87.        /*--- Make sure we have the information ------------------------------*/
  88.        if  SubMatchLength = '' then
  89.            leave;
  90.  
  91.        /*--- Report the information -----------------------------------------*/
  92.        Index = Index + 1;
  93.        say '   * Match (' || Index || ') starts at posn ' || SubMatchStart || ' and is ' || SubMatchLength || ' bytes long.';
  94.    end;
  95. end;
  96.  
  97. /*--- Make change -----------------------------------------------------------*/
  98. say ''
  99. say 'RegExpReplace';
  100. say '~~~~~~~~~~~~~';
  101. Before = "xxx\0yyy"
  102. say 'Replacing: ' || Before
  103. Answer = '?';
  104. MatchRc = RegExpReplace(Before, "Answer");
  105. say 'ReplaceRc = ' || MatchRc;
  106. say 'Answer    = ' || Answer;
  107.  
  108. /*--- Close Regular expression ----------------------------------------------*/
  109. say '';
  110. say 'RegExpCompile(ReClose)';
  111. say '~~~~~~~~~~~~~~~~~~~~~~';
  112. say RegExpCompile("ReClose");
  113. exit(0);
  114.  
  115.  
  116. /*===========================================================================*/
  117. RegExpOk:
  118. /*                                                                           */
  119. /* None of the rexx 'Rx' functions (add/drop/query) work correctly.  The     */
  120. /* return code can't be trusted.                                             */
  121. /*                                                                           */
  122. /* This routine will return 'Y' if the DLL can be accessed.  It calls one    */
  123. /* of the known functions.  We have already registered it so if we fail then */
  124. /* the registration failed.                                                  */
  125. /*                                                                           */
  126. /* Note that as this code is within a subroutine the trap handler we set up  */
  127. /* does not override and set up in the calling code once this routine        */
  128. /* returns.                                                                  */
  129. /*===========================================================================*/
  130.    /*--- Get up trap handler and execute the command ------------------------*/
  131.    signal on SYNTAX  name RegExpNotOk;
  132.    interpret "DummyReRc = RegExpVersion('ReVersion')";
  133.  
  134.    /*--- We did not die so the function must be available -------------------*/
  135.    return('Y');
  136.  
  137.    /*--- We must have died so the function is unavailable -------------------*/
  138.    RegExpNotOk:
  139.    return('N');
  140.  
  141.  
  142.  
  143. /*===========================================================================*/
  144. GetLineNumber:
  145. /*===========================================================================*/
  146.    return( SIGL );
  147.  
  148.  
  149.