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!!


IndietrasherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.