我正在玩std :: regex,并注意到如果字符串出于某种原因来自ifstream,它就不起作用。如果我解开ifstream代码,正则表达式就会停止工作;即使字符串值相同。
/* c++ -Wc++11-extensions -stdlib=libc++ -std=c++11 powerball.cpp -o powerball
example file content..
Draw Date WB1 WB2 WB3 WB4 WB5 PB PP
09/25/2013 49 07 17 53 02 23
09/21/2013 58 54 45 17 12 13
09/18/2013 07 10 35 32 22 19
*/
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
using namespace std;
int main(int argc, char* argv[])
{
string line = "09/25/2013 49 07 17 53 02 23";
regex pbrx("^(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+).*");
/* doesn't work if I do this - even though the content is the same
ifstream ifs("pb-winnums-text.txt");
getline(ifs,line);
getline(ifs,line);
cout << line << endl;*/
smatch matches;
cout << "m.. " << regex_match(line, pbrx) << endl;
if (!regex_match(line, matches, pbrx)) {
cout << "NOPE!" << endl;
}
for (size_t m = 0; m < matches.size(); m++) {
cout << m << ": " << matches[m].str() << endl;
}
return 0;
}
有什么想法吗?