小编Leo*_*olo的帖子

为什么regex_match抛出"复杂性异常"?

我试图测试(使用boost::regex)文件中的行是否仅包含空格分隔的数字条目.我遇到了一个我不理解的异常(见下文).如果有人能够解释为什么抛出它会很棒.也许我在定义模式的方式上做了一些愚蠢的事情?这是代码:

// regex_test.cpp
#include <string>
#include <iostream>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;

int main(){
  // My basic pattern to test for a single numeric expression
  const string numeric_value_pattern = "(?:-|\\+)?[[:d:]]+\\.?[[:d:]]*";
  // pattern for the full line
  const string numeric_sequence_pattern = "([[:s:]]*"+numeric_value_pattern+"[[:s:]]*)+";

  regex r(numeric_sequence_pattern);
  string line= "1 2 3 4.444444444444";
  bool match = regex_match(line, r);
  cout<<match<<endl;

  //...
}
Run Code Online (Sandbox Code Playgroud)

我成功编译了

g++ -std=c++11 -L/usr/lib64/ -lboost_regex regex_test.cpp  
Run Code Online (Sandbox Code Playgroud)

由此产生的程序到目前为止工作正常match == true,我想要.但后来我测试了输入线

string line= "1 2 3 4.44444444e-16"; 
Run Code Online (Sandbox Code Playgroud)

当然,我的模式不是为了识别格式 …

c++ regex boost-regex

5
推荐指数
1
解决办法
2287
查看次数

从字符串读入stringstream

当我尝试从字符串中解析空白分隔的double值时,我发现了这种奇怪的行为,即以循环方式读出字符串.

这是程序:

stringstream ss;
string s("1 2 3 4");
double t;
list<double> lis;

for(int j=0; j!=10; ++j){
    ss << s;
    ss >> t;
    lis.push_back(t);
}

for(auto e : lis){
    cout << e << " ";
}
Run Code Online (Sandbox Code Playgroud)

这里输出:

1 2 3 41 2 3 41 2 3 41
Run Code Online (Sandbox Code Playgroud)

如果我追加尾随空间,s= "1 2 3 4 ";我得到

1 2 3 4 1 2 3 4 1 2 
Run Code Online (Sandbox Code Playgroud)

现在问题:
1)如果我不知道字符串s中有多少条目,我如何读入列表中的所有条目?
2)operator<<我实际上是在打电话ss << s;?是否指定循环阅读?
3)我可以更好地解析吗?

谢谢!


这是固定代码(感谢timrau):

// …
Run Code Online (Sandbox Code Playgroud)

c++ string stringstream

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×2

boost-regex ×1

regex ×1

string ×1

stringstream ×1