[英]Regex to extract value between a single quote and parenthesis using boost token iterator
I have a value like this: 我有一个像这样的值:
Supoose I have a string: 支持我有一个字符串:
s = "server ('m1.labs.teradata.com') username ('u\'se)r_*5') password('uer 5') dbname ('default')";
I need to extract 我需要提取
'm1.labs.teradata.com'
token1: 'm1.labs.teradata.com'
'u\\'se)r_*5'
token2: 'u\\'se)r_*5'
'uer 5'
token3: 'uer 5'
I am using the following regex in cpp: 我在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;
If you do not expect symbol '
inside your string then '[^']+'
would match what you need: 如果您不希望字符串中包含符号'
则'[^']+'
将与您需要的匹配:
regex re("'[^']+'");
live example Result: 实时示例结果:
the token is 'FooBar'
the token is 'Another Value'
There were 2 tokens found.
if you do not need single quotes to be part of match change code to: 如果不需要单引号作为匹配项的一部分,请将更改代码更改为:
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.
The correct regex for this string would be 此字符串的正确正则表达式为
(?:'(.+?)(?<!\\)')
https://regex101.com/r/IpzB80/1 https://regex101.com/r/IpzB80/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.