C ++:vector <string>与正则表达式不匹配

时间:2017-11-20 10:43:09

标签: c++

我真的不知道我的正则表达式不匹配是什么问题。

我尝试了两个代码:

Code1(NOT MATCH):

我正在从文件推送到vector<string> cn;

  //FILE
  ifstream file(params.file_name);
  string line;
  // read each line of the file
  while ( getline(file, line) ){

    istringstream iss(line);
    string token;
    unsigned int loopCsv = 0;
    while (getline(iss, token, ';')){ //when ";" separate cn;uid;mail
      if (loopCsv == 0)
        cn.push_back(token); //cn
      if (loopCsv == 1)
        uid.push_back(token); //uid
      if (loopCsv == 2)
        mail.push_back(token); //mail
      loopCsv++;
      if (loopCsv == 3) //after 3 (cn,uid,mail) repeat
        loopCsv=0;
    }
  }

然后尝试正则表达式:

cout << "There is Neruda Jakub: " << cn[286] << endl;
regex regexX(".*Jakub", std::regex::ECMAScript | std::regex::icase);
bool match = regex_search(cn[286], regexX);
if (match)
  cout << "MATCH!" << endl;

我正在获得输出 有Neruda Jakub:Neruda Jakub

但没有匹配。我还尝试在cn [286]周围添加一些符号,如果没有任何空间的话| Neruda Jakub |并且没有

Code2(MATCH):

vector<string> someVctr;
someVctr.push_back("Neruda Jakub");
regex regexX(".*Jakub", std::regex::ECMAScript | std::regex::icase);
bool match = regex_search(someVctr[0], regexX);
if (match)
 cout << "MATCH!" << endl;

没问题,我会得到MATCH!

我将非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

从评论中看起来该文件被编码为utf-16。所有这一切的简单修复是用char(或Windows上的char16_t)替换所有wchar_t流和字符串

basic_string<char16_t> cn;
basic_string<char16_t> uid;
basic_string<char16_t> mail;

//FILE
basic_ifstream<char16_t> file(params.file_name);
basic_string<char16_t> line;
// read each line of the file
while ( getline(file, line) ){

  basic_istringstream<char16_t> iss(line);
  basic_string<char16_t> token;
  unsigned int loopCsv = 0;
  while (getline(iss, token, ';')){ //when ";" separate cn;uid;mail
    if (loopCsv == 0)
      cn.push_back(token); //cn
    if (loopCsv == 1)
      uid.push_back(token); //uid
    if (loopCsv == 2)
      mail.push_back(token); //mail
    loopCsv++;
    if (loopCsv == 3) //after 3 (cn,uid,mail) repeat
      loopCsv=0;
  }
}

cout << "There is Neruda Jakub: " << cn[286] << endl;
basic_regex<char16_t> regexX(".*Jakub", std::regex::ECMAScript | std::regex::icase);
bool match = regex_search(cn[286], regexX);
if (match)
  cout << "MATCH!" << endl;