1

我在玩 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;
}

有任何想法吗?

4

0 回答 0