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

时间:2017-07-19 13:37:42

标签: c++ regex tokenize

我有这样的价值:

Supoose我有一个字符串:

s = "server ('m1.labs.teradata.com') username ('u\'se)r_*5') password('uer 5')  dbname ('default')";

我需要提取

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

2 个答案:

答案 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});

another live example

the token is   FooBar
the token is   Another Value
There were 2 tokens found.

答案 1 :(得分:0)

此字符串的正确正则表达式为

(?:'(.+?)(?<!\\)')

https://regex101.com/r/IpzB80/1