C++11简单正则表达式不适用于gcc 4.9.1

C++11 simple regex not working with gcc 4.9.1

本文关键字:gcc 适用于 简单 正则表达式 不适用 C++11      更新时间:2023-10-16

我写的简单regex有问题:

std::regex reg = std::regex("(.*)[a-zA-Z]+(.*)");

它在一个简单的程序中工作,比如

#include <regex>
#include <exception>
#include <iostream>
int main(int argc, char *argv[])
{
  std::regex reg = std::regex("(.*)[a-zA-Z]+(.*)");
  std::string s("string");
  if(std::regex_match(s, reg)){
    std::cout << "MATCH !" << std::endl;
  }
  return 0;
}

但我有一个文件,我想用这个正则表达式处理它,但它不起作用。文件内容:

313801.91 323277.59 861.89
313770.97 323272.13 868.89
Start
End
313793.19 323290.19 864.89

我的程序:完整的程序代码;输入文件

  ...
  std::ifstream input;
  std::string cell;
  std::regex reg = std::regex("(.*)[a-zA-Z]+(.*)");
  input.open("a.txt", std::ofstream::in);
  while (std::getline(input, cell))
  {
    // reject the row if there is any text in it
    if( std::regex_match(cell, reg) ){
      // never hit!
      continue;
    }
  }
  ...

当我想把它打印出来时,我得到了一个奇怪的结果:

cout << "_____"" << cell << ""____" << endl;
"____"Start

问题在于行尾。我保存了一个以CLRF行结尾的文件(可能是在Windows上)。

我更新了我的正则表达式,现在它工作:

std::regex reg("(.*)[a-zA-Z]+(.*)(rn|n|r)");