2

I have a string:

s = "server ('m1.labs.terada')ta.com') username ('user5') password('user)5') dbname ('default')";

I am extracting the argument names: such as server, username..., dbname.

To do this I am using the following regex:

regex re("\\(\'[!-~]+\'\\)");
sregex_token_iterator i(s.begin(), s.end(), re, -1);
sregex_token_iterator j;
unsigned count = 0;
while(i != j)
{
    string str1 = *i;
    cout <<"token = "<<str1<< endl;
    i++;
    count++;
}
cout << "There were " << count << " tokens found." << endl

For this the output I am getting is :

token = server 
token =  username 
token =  password
token =  dbname 
token =  
There were 5 tokens found.

How shall I avoid the 5th token formed. Am I missing out on anything?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
hydra123
  • 337
  • 5
  • 14

1 Answers1

1

It turns out there are some non-word chars at the end of the string. You may match them with \W* (zero or more non-word chars). Since your tokens are only composed of word chars, you may safely wrap your pattern with \W* pattern:

regex re("\\W*\\(\'[!-~]+\'\\)\\W*");

See the C++ online demo

Result:

token = server
token = username
token = password
token = dbname
There were 4 tokens found.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563