ILE RPG RegEx API

465 pts.
Tags:
AS/400
RPG ILE
Hi All, Has anyone had any luck implementing the Regex C APIs within ILE RPG? I'm aware of the matter of converting the pattern input to match CCSID 37, and I thought I had catered for the gotchas. But I cannot get the API to work for anything but the most basic of match conditions. For example if I specify a string 'Andy 768023270 Andy' and a Regex pattern containing just 'Andy', I get a result. If I change the Regex pattern to '(backslash)d{9}' (find a 9-digit number) it fails. The hex representation of that regex is E084C0F9D0 which to me looks to match CCSID 37. I'll post up the code if any help - just didn't want to overload this first post. Thanks in advance. Andy

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 5  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • andyhumphreys
    Here's the code (experimental service program):
         h debug datedit(*ymd) nomain bnddir('QC2LE')
          *
          *****************************************************************
          * Procedure Interface(s)                                        *
          *****************************************************************
          *
         dRegex            pr          4500
         dStringIn                     2000
         dRegExpIn                      500
          *
          *****************************************************************
          * Sub-procedure Regex                                           *
          *****************************************************************
          *
         PRegex            b                   export
         d                 pi          4500
         dStringIn                     2000
         dRegExpIn                      500
          *
          *****************************************************************
          * Variable Specifications                                       *
          *****************************************************************
          *
         d retval          ds
         d  result1                1   2000
         d  result2             2001   4000
         d  error               4001   4500
          *
         D regex_t         DS                  align
         D  re_nsub                      10I 0
         D  re_comp                        *
         D  re_cflags                    10I 0
         D  re_erroff                    10I 0
         D  re_len                       10I 0
         D  re_ucoll                     10I 0 dim(2)
         D  re_lsub                        *   DIM(9)
         D  re_esub                        *   DIM(9)
         D  re_map                      256A
         D  re_shift                      5I 0
         D  re_dbcs                       5I 0
         D regmatch_t      DS                  occurs(maxEntry) align
         D  rm_so                        10I 0
         D  rm_ss                         5I 0
         D  rm_eo                        10I 0
         D  rm_es                         5I 0
         D regcomp         PR            10I 0 extproc('regcomp')
         D   preg                          *   value
         D   pattern                       *   value options(*string)
         D   cflags                      10I 0 value
         D regexec         PR            10I 0 extproc('regexec')
         D   preg                          *   value
         D   string                        *   value options(*string)
         D   nmatch                      10U 0 value
         d   pmatch                        *   value
         D   eflags                      10I 0 value
         D regerror        PR            10U 0 extproc('regerror')
         D   errcode                     10I 0 value
         D   preg                          *   value
         D   errbuf                        *   value
         D   errbuf_size                 10I 0 const
         D regfree         PR                  extproc('regfree')
         D   preg                          *   value
         D maxEntry        c                   const(10)
         D preg            S               *
         D pmatch          S               *
         D string          S           2000A
         D len             S             10I 0
         D rc              S             10I 0
         D nmatch          S             10U 0 inz(maxentry)
         D buf             S            256A
         D pattern         S            500A
         D temp            s            100a   varying
         D cflags          s             10i 0
         D x               s             10i 0
         D y               s              4s 0 inz(1)
         d/copy regex_h
          /free
    
           *inlr = *on;
    
           string = %trim(stringin);
           pattern = %trim(regexpin);
           dou y = %len(%trim(pattern));
           if %subst(pattern:y:1) = x'B1';
           pattern = %replace(x'BA':pattern:y:1);
           endif;
           y = y + 1;
           enddo;
           %occur(regmatch_t) = 1;
           preg = %addr(regex_t);
           pmatch = %addr(regmatch_t);
    
           // Compile RE
           cflags = REG_BASIC + REG_ICASE + REG_NOSUB;
           rc = regcomp(preg: %trim(pattern): cflags);
           if rc <> 0;
             regerror(rc: preg: %addr(buf): %size(buf));
             temp = 'comp() failed with: ' + %str(%addr(buf));
             error = temp;
             return retval;
           endif;
           // Execute RE
           rc = regexec(preg: %trim(string): nmatch: pmatch: 0);
           if rc <> 0;
             regerror(rc: preg: %addr(buf): %size(buf));
             temp = 'regexec() failed with: ' + %str(%addr(buf));
             error = temp;
           dump;
             regfree(preg);
             return retval;
           endif;
           for x = 1 to nmatch;
             %occur(regmatch_t) = x;
    
             // Done processing if these are negative
             if rm_eo = -1 or rm_so = -1;
               leave;
             endif;
             len = rm_eo - rm_so;
             rm_so = rm_so + 1;
             temp = %subst(string: rm_so: len);
             result1 = temp;
           endfor;
    
           regfree(preg);
    
           return retval;
          /END-FREE
          *
         p                 e
    
    465 pointsBadges:
    report
  • BigKat
    I played around with these for a while in the past. Never could get the complex patterns to work either.
    8,200 pointsBadges:
    report
  • TomLiotta
    I'm aware of the matter of converting the pattern input to match CCSID 37... AFAIK, it's not a matter of 'matching CCSID 37'. It's a matter of handling your characters in the appropriate CCSID which might be CCSID 37. For your uploaded source, what CCSID is your program compiled for? What CCSID is your source created as? What CCSID does your job have when the program is called? Tom
    125,585 pointsBadges:
    report
  • andyhumphreys
    Hi Tom, 37 for the source file, 65535 for the job normally, but I should point out that I have already tried changing the JOB CCSID to 37 both when compiling the service program module and it's calling program AND running it under CCSID 37, this makes no difference to the outcome :(
    465 pointsBadges:
    report
  • TomLiotta
    I had time to test your code, and got the same result. But simply running the code is all the time I've had for it since you first posted. I don't know when/if I'll get time for anything more than a few minutes at a time. Life is too crowded during recent weeks for more than short interactions at a time, and this needs some dedication to verify each detail. It looks right by simple eye-checking. Wish I could find time for detail work.  Tom
    125,585 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following