• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 563
  • Last Modified:

Use regular expressions (need to parse c++ code)

Hi experts!


I need to retireve certain information from a string in c++. The string is:

"DRIVER={SQL Server};SERVER=apspldb01;DATABASE=clients;UID=sa;PWD=123"

from this string I need to retireve the SERVER, DATABASE, UID and PWD. I wanted to use regular expressions but I was unable to find a tutorial for using regexp in c++.

Help very appreciated!!


0
Indietrasher
Asked:
Indietrasher
1 Solution
 
inq123Commented:
Hi Indietrasher,

That really depends on the library you use.  If you use PCRE (which I prefers since perl regex is easy to learn and supported by many languages), then you just need PCRE man pages (http://www.pcre.org/pcre.txt), and perl regex syntax (http://www.perldoc.com/perl5.8.0/pod/perlre.html).

Cheers!
0
 
jkrCommented:
You don't need regular expressions for this task, that would be overkill.
#include <string.h>

void GetTokenValue ( char* pszLine, char* pszToken, char* pszValue, int nValLen) {

    char* pTmp = strdup(pszLine);
    char* pszSeps = ";";

    char* p = strtok(pTmp,pszSeps);

    while(p) {

        if (p == strstr(p,pszToken)) { // found token at the beginning of the substring

            size_t szToken = strlen(pszToken);

            if ( strlen(p + szToken + 1) < nValLen) { // check if space is sufficiant

                strcpy(pszValue, p + szToken + 1); // copy data after '='

                break;
            }
        }
    }

    free(p);
}

and use it like

char* pszLine = "DRIVER={SQL Server};SERVER=apspldb01;DATABASE=clients;UID=sa;PWD=123";

char acDriver [ 255];

GetTokenValue ( pszLine, "DRIVER", acDriver, sizeof(acDriver));
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rstaveleyCommented:
I'll second jkr's comment about regex being overkill under these circumstances, but I'd go for something along the lines of:
--------8<--------
#include <iostream>
#include <sstream>
#include <map>

typedef std::map<std::string,std::string> StringMap;
StringMap mapParameters(const std::string& str);

int main()
{
      StringMap map = mapParameters("DRIVER={SQL Server};SERVER=apspldb01;DATABASE=clients;UID=sa;PWD=123");
      std::cout << "SERVER is " << map["SERVER"] << '\n';
}

StringMap mapParameters(const std::string& str)
{
      std::istringstream is(str);
      std::string assignment;
      StringMap map;
      while (getline(is,assignment,';')) {
            std::string::size_type pos = assignment.find('=');
            if (pos != std::string::npos && pos+1 < assignment.size())
                  map[assignment.substr(0,pos)] = assignment.substr(pos+1);
      }
      return map;
}
--------8<--------
0
 
IndietrasherAuthor Commented:
Thank you all for your help!

I forgot to mention that I'm using Borland 5 C++ (or Turbo C++ 4.5) so I dont think I can use Perl.

JRK,


I'm having a bit of a problem understanding the function you posted...

what does - strdup(pszLine); - and - strtok(pTmp,pszSeps) - do?

and (if it is not a problem) could you define what does your variables mean? (  pTmp, pszLine, pszSeps)  etc

Thak you so much!!!!!!
0
 
jkrCommented:
>>I'm having a bit of a problem understanding the function you posted...
>>what does - strdup(pszLine); - and - strtok(pTmp,pszSeps) - do?

'strdup(pszLine)' creates a temporary copy of the line passed in and 'strtok(pTmp,pszSeps)' tokenizes the line with ';' trated as a field separator.

>>and (if it is not a problem) could you define what does your variables mean?

Sure,

void GetTokenValue ( char* pszLine, char* pszToken, char* pszValue, int nValLen) {

   char* pTmp = strdup(pszLine); // create a temp. local copy of the line passed in
   char* pszSeps = ";"; // use ';' as the field separator

   char* p = strtok(pTmp,pszSeps); // look for our separators and store the tokens in 'p'

  while(p) {

       if (p == strstr(p,pszToken)) { // found token at the beginning of the substring

           size_t szToken = strlen(pszToken);

           if ( strlen(p + szToken + 1) < nValLen) { // check if space is sufficiant

               strcpy(pszValue, p + szToken + 1); // copy data after '='

               break;
           }
       }

       p = strtok(NULL,pszSeps); // this line was missing, continue tokenizing if ours was not found
   }

   free(pTmp); // free temp. copy
}
0
 
IndietrasherAuthor Commented:
jkr,

Thanks a lot for the explanation. I tried the code and it worked great, but there is only one problem...when retrieving the PWD value, I am getting as a result 123" and I just can't modify it to ignore the " .

any ideas?
0
 
jkrCommented:
Um, sorry, but if you have


char* pszLine = "DRIVER={SQL Server};SERVER=apspldb01;DATABASE=clients;UID=sa;PWD=123";

there is no trailing " in the string that coule be found... unless you were explicitly setting that by using

char* pszLine = "DRIVER={SQL Server};SERVER=apspldb01;DATABASE=clients;UID=sa;PWD=123\"";
0
 
IndietrasherAuthor Commented:
jkr,

The hting is that the string I needed to analize is

"\"DRIVER={SQL Server};SERVER=apspldb01;DATABASE=clients;UID=sa;PWD=123\"";

but anyway I managed to change the function you posted! here it is:

void GetTokenValue (char* pszLine, char* pszToken, char* pszValue, int nValLen){

char* pTmp = strdup(pszLine);
char* pszSeps = ";";
char* p = strtok(pTmp,pszSeps);

int where;
string newLine;


while(p) {

   if (p == strstr(p,pszToken)) {
         size_t szToken = strlen(pszToken);

         if ( strlen(p + szToken + 1) < nValLen) {

              strcpy(pszValue, p + szToken + 1);
             newLine = pszValue;
             where =  newLine.find("\"");
             if (where >= 0 ){            
      newLine.resize(strlen(pszValue) - 1);
      strcpy(pszValue,newLine.c_str());
      }

           break;
         }
    }

 p = strtok(NULL,pszSeps);

}
}




That'll take the extrs " at the end....

Thanks a lot for your help JKR!


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now