It is true that the C ++ regular expression library for POSIX does not have an assembly in the regexp replacement function, but you can do the same using positional information from regexec() and the built-in RPGLE %replace() function. (I assume you are going to use RPGLE, but you can use a different language.)
For example, if you want to hide everything except the last four digits of a phone number, you can do this:
/include qcpysrc,regex_h d regex_phone_number... d ds inz likeds(regex_t) d dsrm ds inz likeds(regmatch_t) dim(20) d data s 52a inz varying d pattern s 256a inz varying d rc s 10i 0 inz(0) /FREE *inlr = *on ; data = 'My phone #' are: (444) 555 - 6666 and 777.888.9999' ; dsply data ; pattern = '\(?([0-9]{3})[ .)]*([0-9]{3})[ .-]*([0-9]{4})' ; rc = regcomp(regex_phone_number :pattern :REG_EXTENDED) ; if rc = 0 ; dow '1' ; rc = regexec(regex_phone_number :data :regex_phone_number.re_nsub :%addr(dsrm) :0) ; if rc <> 0 ; leave ; endif ; data = %replace('***': data :dsrm(2).rm_so+1 :dsrm(2).rm_eo - dsrm(2).rm_so) ; data = %replace('***': data :dsrm(3).rm_so+1 :dsrm(3).rm_eo - dsrm(3).rm_so) ; enddo ; endif ; dsply data ; regfree(regex_phone_number) ; /END-FREE
Here is what a copy of regex_h looks like:
** Header file for calling the "Regular Expression" functions ** provided by the ILE C Runtime Library from an RPG IV ** program. Scott Klement, 2001-05-04 ** Converted to qualified DS 2003-11-29 ** Modified by Jarrett Gilliam 2014-11-05 ** ** This copy book is for using the C regular expression library, regex.h, in RPG. ** You can go to http:
Here's the conclusion:
DSPLY My phone
The code could be improved by extracting the replacement logic and putting it in its own procedure, creating a custom regular expression replacement function based on the POSIX library, but this is not entirely necessary.
source share