我有这样的价值:
Supoose我有一个字符串:
s = "server ('m1.labs.teradata.com') username ('u\'se)r_*5') password('uer 5') dbname ('default')";
我需要提取
'm1.labs.teradata.com'
'u\'se)r_*5'
'uer 5'
我在cpp中使用以下正则表达式:
regex re("(\'[!-~]+\')");
sregex_token_iterator i(s.begin(), s.end(), re, 0);
sregex_token_iterator j;
unsigned count = 0;
while(i != j)
{
cout << "the token is"<<" "<<*i++<< endl;
count++;
}
cout << "There were " << count << " tokens found." << endl;
return 0;
答案 0 :(得分:2)
如果您不希望字符串中包含符号'
,则'[^']+'
会匹配您所需的内容:
regex re("'[^']+'");
live example 结果:
the token is 'FooBar'
the token is 'Another Value'
There were 2 tokens found.
如果您不需要单引号作为匹配更改代码的一部分:
regex re("'([^']+)'");
sregex_token_iterator i(s.begin(), s.end(), re, {1});
the token is FooBar
the token is Another Value
There were 2 tokens found.
答案 1 :(得分:0)