简体   繁体   English

正则表达式使用Boost令牌迭代器在单引号和括号之间提取值

[英]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 我需要提取

  • token1 : 'm1.labs.teradata.com' token1: 'm1.labs.teradata.com'
  • token2 : 'u\\'se)r_*5' token2: 'u\\'se)r_*5'
  • token3 : '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});

another live example 另一个现场例子

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.

相关问题 提取在括号和单引号之间使用单引号引起来的字符串 - extract a string with single quotes between parenthesis and single quote Boost 正则表达式令牌迭代器:在括号之间获取输入 - Boost regex token iterator: getting input between parentheses regex_token_iterator和regex_iterator有什么区别? - What is the difference between regex_token_iterator and regex_iterator? 表达式:字符串迭代器在使用 boost 正则表达式时不可解引用 - Expression: string iterator not dereferencable while using boost regex Boost Single Pass Iterator和Forward Traversal Iterator之间的区别是什么? - What's the difference between a Boost Single Pass Iterator and a Forward Traversal Iterator? 使用boost :: iterator - Using boost::iterator 使用boost multi_array迭代器在数组元素之间分配 - assignment between array elements using boost multi_array iterator 如何在 C++ 中使用正则表达式来提取包含括号之间空格的文本 - How can I use regex in C++ to extract text including spaces between parenthesis C ++基于/使用(增强型)正则表达式拆分字符串以找到令牌 - C++ Split string based on/using (boost) regex to find the token 如何将boost :: spirit :: lex标记的值从iterator_range转换为字符串? - How do I convert a boost::spirit::lex token's value from iterator_range to a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM