正则表达式使用提升令牌迭代器在单引号和括号之间提取值
Regex to extract value between a single quote and parenthesis using boost token iterator
我有一个这样的值:
Supoose 我有一个字符串:
s = "server ('m1.labs.teradata.com') username ('u'se)r_*5') password('uer 5') dbname ('default')";
我需要提取
- 令牌 1 :
'm1.labs.teradata.com'
- 令牌 2 :
'u'se)r_*5'
- 令牌3 :
'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;
如果您不希望字符串中包含符号'
,那么'[^']+'
将匹配您的需求:
regex re("'[^']+'");
现场示例 结果:
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.
此字符串的正确正则表达式是
(?:'(.+?)(?<!\)')
https://regex101.com/r/IpzB80/1
相关文章:
- 使用正则表达式c++从单词和分隔符之间的字符串中提取所有子字符串
- 如何从txt文件c++中提取括号之间的数字
- 派生类 插入和提取运算符重载以及基类与派生类之间的强制转换
- 正则表达式使用提升令牌迭代器在单引号和括号之间提取值
- C++提取两个字符串之间的数据
- 提取 XML 标记中双 qoutes 之间的子字符串
- 提取 C++ 中分量之间的子字符串
- 如何从两个字符之间的文件中提取整数
- 如何提取C 中的字符/字符串之间的字符串
- 用于提取[]之间内容的正则表达式
- 提取两个单词之间的域
- C++:从两个符号之间提取字符串
- 如何提取两个括号之间存在的字符串
- 从隐藏在随机字符之间的文件中提取整数
- 提取符号之间的字符串
- 将两个 '' 字符之间的子字符串提取到单独的数组中
- c ++ 如何提取单词之间的空格(如果有)
- 从c++中的字符串中提取两个分隔符之间的子字符串
- c++如何提取两个相同符号之间的子字符串
- 如何在同一级别的大括号之间提取字符串