avoid empty token in cpp

心不动则不痛 提交于 2019-12-12 21:09:34

问题


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?


回答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.


来源:https://stackoverflow.com/questions/44646868/avoid-empty-token-in-cpp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!