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

C++: vector<string> does not match regex

本文关键字:正则表达式 不匹配 gt string lt C++ 矢量      更新时间:2023-10-16

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

我尝试了两个代码:

代码 1(不匹配(:

我正在从文件推送到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;

我正在获得输出:有聂鲁达·雅库布:聂鲁达·雅库布

但没有匹配。如果没有任何空格,我还尝试在 cn[286] 周围添加一些符号 |聂鲁达·雅各布|而且没有

代码 2 (匹配(:

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;

没问题,我会得到匹配!

如果您能提供任何帮助,我将不胜感激。

从注释来看,该文件似乎被编码为 utf-16 .所有这些的简单解决方法是将所有char流和字符串替换为char16_t(或Windows上的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;