C++:矢量<string>与正则表达式不匹配
C++: vector<string> does not match regex
我真的不知道我的正则表达式不匹配有什么问题。
我尝试了两个代码:
代码 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;
相关文章:
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 查找中间两个数字的正则表达式的匹配项
- C++ 正则表达式不匹配空格
- 无法使用提升正则表达式查找匹配项
- 调试正则表达式不匹配
- 如何编写正则表达式以匹配以下数字格式?
- 为什么regex_match与我的正则表达式不匹配
- C++:矢量<string>与正则表达式不匹配
- 仅查找第一个 std::正则表达式有效匹配
- qt中的正则是不匹配字符串
- 提升正则表达式不匹配
- 正则表达式不允许在 dot(.) 之后立即写入 dot(.)
- Regex分组与C++11正则表达式库匹配
- 我的 XML 标签正则表达式不起作用
- 使用 libc++ 正则表达式库匹配"beginning-of-line" (C++11)
- C++11正则表达式不匹配任何内容
- 简单正则表达式不匹配
- Qt正则表达式不匹配
- 正则表达式IfThenElse -匹配单个闭括号或不匹配括号
- POSIX正则表达式不匹配