midrange.com code scratchpad
Name:
Matt Hopkins
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
08/24/2009 06:49:57 pm
IP:
Logged
Description:
Procedure to take a text string and a regex expression and return if there is a match.

Based on code from here: http://www.think400.dk/adhoc_3.htm#eks0003

and here: http://code.midrange.com/492f7222d1.html
Code:
  1.      H BNDDIR('QC2LE') DEBUG
  2.      H nomain
  3.  
  4.      D regex_t         DS                  qualified  dim(1) align
  5.      D  re_nsub                      10U 0
  6.      D  re_comp                        *   inz(*NULL)
  7.      D  re_cflags                    10I 0
  8.      D  re_erroff                    10U 0
  9.      D  re_len                       10U 0
  10.      D  re_ucoll                     10I 0 Dim(2)
  11.      D  re_lsub                        *   Inz(*NULL)
  12.      D  lsub_ar                      10I 0 Dim(16)
  13.      D  esub_ar                      10I 0 Dim(16)
  14.      D  reserved1                      *   Inz(*NULL)
  15.      D  re_esub                        *   Inz(*NULL)
  16.      D  re_specchar                    *   Inz(*NULL)
  17.      D  re_phdl                        *   Inz(*NULL)
  18.      D  comp_spc                      1A   Dim(112)
  19.      D  re_map                        1A   Dim(256)
  20.      D  re_shift                      5I 0
  21.      D  re_dbcs                       5I 0
  22.  
  23.  
  24.      D* regex_t         DS                  align
  25.      D*  re_nsub                      10I 0
  26.      D*  re_comp                        *
  27.      D*  re_cflags                    10I 0
  28.      D*  re_erroff                    10I 0
  29.      D*  re_len                       10I 0
  30.      D*  re_ucoll                     10I 0 dim(2)
  31.      D*  re_lsub                        *   DIM(9)
  32.      D*  re_esub                        *   DIM(9)
  33.      D*  re_map                      256A
  34.      D*  re_shift                      5I 0
  35.      D*  re_dbcs                       5I 0
  36.  
  37.      D regmatch_t      DS                   occurs(maxEntry) align
  38.      D  rm_so                        10I 0
  39.      D  rm_ss                         5I 0
  40.      D  rm_eo                        10I 0
  41.      D  rm_es                         5I 0
  42.  
  43.      D regcomp         PR            10I 0 extproc('regcomp')
  44.      D   preg                          *   value
  45.      D   pattern                       *   value options(*string)
  46.      D   cflags                      10I 0 value
  47.  
  48.      D regexec         PR            10I 0 extproc('regexec')
  49.      D   preg                          *   value
  50.      D   string                        *   value options(*string)
  51.      D   nmatch                      10U 0 value
  52.      d   pmatch                        *   value
  53.      D   eflags                      10I 0 value
  54.  
  55.      D regerror        PR            10U 0 extproc('regerror')
  56.      D   errcode                     10I 0 value
  57.      D   preg                          *   value
  58.      D   errbuf                        *   value
  59.      D   errbuf_size                 10I 0 value
  60.  
  61.      D regfree         PR                  extproc('regfree')
  62.      D   preg                          *   value
  63.  
  64.      D maxEntry        c                   const(10)
  65.      D preg            S               *
  66.      D pmatch          S               *
  67.      D len             S             10I 0
  68.      D rc              S             10I 0
  69.      D nmatch          S             10U 0 inz(maxEntry)
  70.      D cflags          S             10I 0
  71.      D Msg             S             50A
  72.      D Buf             S            256A
  73.      D IsMatch         S               n
  74.      D temp            s            100a   varying
  75.      D x               s             10i 0
  76.  
  77.       /COPY PR_REGEX_H
  78.  
  79. /========================================================/
  80. /================PR_REGEX_H - start ==========================/
  81.  
  82.       // regcomp() cflags */
  83.  
  84.       //#define REG_BASIC          0x000   /* Basic RE rules  (BRE)        */
  85.      D REG_BASIC       C                   0
  86.       //#define REG_EXTENDED       0x001   /* Extended RE rules (ERE)      */
  87.      D REG_EXTENDED    C                   1
  88.       //#define REG_ICASE          0x002   /* Ignore case in match         */
  89.      D REG_ICASE       C                   2
  90.       //#define REG_NEWLINE        0x004   /* Convert <backslash><n> to
  91.       //                                                         <newline> */
  92.      D REG_NEWLINE     C                   4
  93.       //#define REG_NOSUB          0x008   /* regexec() not report
  94.       //                                                    subexpressions */
  95.      D REG_NOSUB       C                   8
  96.       //#define REG_ALT_NL         0x010   /* POSIX: Use IFS newline       */
  97.       //                                   /* instead of database newline  */
  98.       //                                   /* UTF32: Use database newline  */
  99.       //                                   /* instead of IFS newline.      */
  100.      D REG_ALT_NL      C                   10
  101.       //
  102.       // regexec() eflags */
  103.  
  104.       //#define REG_NOTBOL  0x100   /* First character not start of line   */
  105.      D REG_NOTBOL      C                   100
  106.       //#define REG_NOTEOL  0x200   /* Last character not end of line      */
  107.      D REG_NOTEOL      C                   200
  108.  
  109.  
  110.  
  111.       // Regular Expression error codes */
  112.  
  113.       //#define REG_NOMATCH        1  /* RE pattern not found           */
  114.      D REG_NOMATCH     C                   1
  115.       //#define REG_BADPAT         2  /* Invalid Regular Expression     */
  116.      D REG_BADPAT      C                   2
  117.       //#define REG_ECOLLATE       3  /* Invalid collating element      */
  118.      D REG_ECOLLATE    C                   3
  119.       //#define REG_ECTYPE         4  /* Invalid character class        */
  120.      D REG_ECTYPE      C                   4
  121.       //#define REG_EESCAPE        5  /* Last character is \            */
  122.      D REG_EESCAPE     C                   5
  123.       //#define REG_ESUBREG        6  /* Invalid number in \digit       */
  124.      D REG_ESUBREG     C                   6
  125.       //#define REG_EBRACK         7  /* imbalance                      */
  126.      D REG_EBRACK      C                   7
  127.       //#define REG_EPAREN         8  /* \( \) or () imbalance          */
  128.      D REG_EPAREN      C                   8
  129.       //#define REG_EBRACE         9  /* \{ \} or { } imbalance         */
  130.      D REG_EBRACE      C                   9
  131.       //#define REG_BADBR         10  /* Invalid \{ \} range exp        */
  132.      D REG_BADBR       C                   10
  133.       //#define REG_ERANGE        11  /* Invalid range exp endpoint     */
  134.      D REG_ERANGE      C                   11
  135.       //#define REG_ESPACE        12  /* Out of memory                  */
  136.      D REG_ESPACE      C                   12
  137.       //#define REG_BADRPT        13  /* ?*+ not preceded by valid RE   */
  138.      D REG_BADRPT      C                   13
  139.       //#define REG_ECHAR         14  /* invalid multibyte character    */
  140.      D REG_ECHAR       C                   14
  141.       //#define REG_EBOL          15  /* (shift 6 caret or not) anchor
  142.       //                                          and not BOL           */
  143.      D REG_EBOL        C                   15
  144.       //#define REG_EEOL          16  /* $ anchor and not EOL           */
  145.      D REG_EEOL        C                   16
  146.       //#define REG_ECOMP         17  /* Unknown error in regcomp() call*/
  147.      D REG_ECOMP       C                   17
  148.       //#define REG_EEXEC         18  /* Unknown error in regexec() call*/
  149.      D REG_EEXEC       C                   18
  150.       //#define REG_LAST          18  /* Needs to always be the greatest*/
  151.       //                              /* regerror uses it to check for  */
  152.       //                              /* valid number                   */
  153.      D REG_LAST        C                   18
  154.  
  155.      D IsRegexMatch    pr              n
  156.      D   InputString                 50a   value
  157.      D   RegexString                 40a   value
  158.  
  159. /================PR_REGEX_H - end  ==========================/
  160. /========================================================/
  161.  
  162.  
  163.      D*********************************************
  164.      p IsRegexMatch    b                   export
  165.      D*********************************************
  166.      D IsRegexMatch    pi              n
  167.      D   InputString                 50a   value
  168.      D   RegexString                 40a   value
  169.  
  170.       /FREE
  171.  
  172.        // Initialize pointers
  173.        %occur(regmatch_t) = 1;
  174.        preg = %addr(regex_t);
  175.        pmatch = %addr(regmatch_t);
  176.        ismatch = *on;
  177.  
  178.        // Compile RE
  179.        cflags = REG_EXTENDED + REG_ICASE + REG_NOSUB;
  180.        rc=regcomp(preg:%trim(RegexString):cflags);
  181.        if rc <> 0;
  182.          regerror(rc: preg: %addr(buf): 256);
  183.          temp = 'regcomp() failed with: ' + %str(%addr(buf));
  184.          return IsMatch;
  185.        endif;
  186.  
  187.        // Execute RE
  188.        rc = regexec(preg: %trim(InputString):
  189.            nmatch: pmatch: 0);
  190.        if rc <> 0;
  191.          ismatch = *off;
  192.          regerror(rc: preg: %addr(buf): 256);
  193.          regfree(preg);
  194.          temp = 'regexec() failed with: ' + %str(%addr(buf));
  195.          return IsMatch;
  196.        endif;
  197.  
  198.        // Show results:
  199.         for x = 1 to nmatch;
  200.           %occur(regmatch_t) = x;
  201.  
  202.           // Done processing if these are negative
  203.           if rm_eo = -1 or rm_so = -1;
  204.             leave;
  205.           endif;
  206.  
  207.           len = rm_eo - rm_so;
  208.           rm_so = rm_so + 1;
  209.           temp = %subst(InputString: rm_so: len);
  210.         endfor;
  211.  
  212.         regfree(preg);
  213.  
  214.         return IsMatch;
  215.       /END-FREE
  216.      P                 E                         
© 2004-2019 by midrange.com generated in 0.007s valid xhtml & css