Validating an email address format in CICS with <regex>

By | September 10, 2014

In an earlier post, I used regexec() to validate an email address format and found that regexec() does not support non-capture  groups.  Well, I didn’t really need them, but let’s see if we can get this support by using the Standard C++ Library <regex> template function which does support non-capture groups which is part of the ECMAScript standard.  I couldn’t figure a way to use <regex> from COBOL.  So I thought I would tackle writing a C++ CICS program using the CICS Foundation Classes.  I went through a lot or trial and error to fool the compiler, er, get the syntax right as I had trouble finding a good example.  Finally I found A Tr1 Tutorial for a working example of using (non-boost) <regex>.  Thanks!

// Validate an email address format                                     
                                                                        
#pragma runopts(ENVAR(CICSVAR=THREADSAFE))   // Required for XPLINK     
                                                                        
#define ICC_EDF                              // Enable EDF use          
#include "icceh.hpp"                                                    
#include "iccmain.hpp"                                                  
                                                                        
#define __IBMCPP_TR1__ (1)                   // Enable STL TR1          
#include <string>
#include <regex>                                                         
                                                                        
using namespace std;                                                    
                                                                        
void IccUserControl::run()                                              
{                                                                       
  IccTerminal* term = terminal();                                       
  IccBuf       termInput(80);                                           
  string       emailAddress;                                            
  char *       validateEmailPattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]"
    "+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?"
    ":[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";                              
                                                                        
  termInput = term->receive();                                          
  emailAddress = termInput.cut(5);           // Assume 4 char transaction ID
  
  term->erase(); 

  if (std::tr1::regex_match(emailAddress, 
      std::tr1::regex(validateEmailPattern)))
    term->send( 1,1, "Good email ");
  else 
    term->send( 1,1, "Bad email ");

  term->send(emailAddress.c_str());
  term->send( 2,1, "REGEX: "); 
  term->send(validateEmailPattern); term->freeKeyboard();
  return;
}

Compile with…

// JCLLIB ORDER=(CICSTS51.CICS.SDFHPROC)                        
//*                                                                   
//VAPEMCPP EXEC ICCFCGL,OUTC=*,                                       
// LIBPRFX=CEE,LNGPRFX=CBC,INDEX=CICSTS51.CICS,CREGSIZ=96M,     
// CPARM='SOURCE NOCSE LIST OPT ARCH(10) XPLINK',                      
// INFILE=XXXXXXXX.SRC.C(VALEMCPP),                                     
// OUTFILE=XXXXXXXX.CICS.LOADLIB                                     
//LKED.C128   DD DISP=SHR,DSN=CEE.SCEELIB(C128) Std C++ Library XPLINK
//LKED.SYSIN  DD *                                                    
 INCLUDE C128                                                         
 NAME VALEMCPP(R)                                                     

References:
Standard C++ Library Reference
XPLink and C and C++ programming

Leave a Reply

Your email address will not be published. Required fields are marked *