Wednesday, January 22, 2014

regex and regcmp usage in c++ coding

Incase you need to perform a string comparison or format matching using regex and regcmp subroutines.

Like I have a pattern like :
"AB1234/23JAN/MEL/DEL" or "AANNN/NNAAA/AAA/AAA"
If any message fails to satisfy this pattern I need to throw some error..
How do I do that in Unix + C++ env?

Generally in UNIX there is a binary called "regcmp". Actual path is /usr/ccs/bin/regcmp
We will use this binary to get a character pattern from this like below.
Command :
1) I created a file named "regfile".
Below is the content:
[host:deb]{deb}/home/users/deb=>less regfile
a "AB1234/23JAN/MEL/DEL"

regfile (END)
2) Executed command : 
regcmp - regfile
3) It generated a file :
regfile.c
4) Content of the file regfile.c :
[host:deb]{deb}/home/users/deb=>less regfile.c
/* "AB1234/23JAN/MEL/DEL" */
char a[] = {
024,0101,024,0102,024,061,024,062,024,063,024,064,
024,057,024,062,024,063,024,0112,024,0101,024,0116,
024,057,024,0115,024,0105,024,0114,024,057,024,0104,
024,0105,024,0114,064,
0};
5) C++ code:

parseInput(const string& input, string& carrier, string& command)
{

   char input[] = {
          024,0101,024,0102,024,061,024,062,024,063,024,064,
024,057,024,062,024,063,024,0112,024,0101,024,0116,
024,057,024,0115,024,0105,024,0114,024,057,024,0104,
024,0105,024,0114,064,
0};

   char buffering[21];
   memset(buffering,0,20);

   if (regex(input,(char*)input.c_str(),buffering)==NULL)
   {
      cout >> "Invalid input - required format is AAnnnn/nnAAA/AAA/AAA.");
   }
}

Thank You.


No comments:

Post a Comment