-3

很抱歉,这类似于家庭作业。但是,我无法使用 std::regex 解析天、月和年。我只是看不到缺少什么。

#include<iostream>
#include<string>
#include<vector>
#include<regex>

int main()
{
    std::vector<std::string> series;
    series.push_back("2013-02-01,54.87,55.20,54.67,54.92,2347600,54.92");
    series.push_back("2013-01-31,54.74,54.97,53.99,54.29,3343300,54.29");
    series.push_back("2013-01-30,54.84,55.35,54.68,54.68,2472800,54.68");

    const std::regex date("(\\d{4})-(\\d{2})-(\\d{2})");
    std::smatch dates;

    for (unsigned int  i = 0; i < series.size() ; i++)
    {
        if (std::regex_match(series[i], dates, date))
            std::cout << dates[1] << "\t" << dates[2] << "\t" << dates[3] << std::endl;
        else
            std::cout << "No match!" << std::endl;
    }

    return 0;
}
4

1 回答 1

4

为此,请使用regex_search,而不是regex_matchregex_match要求正则表达式匹配整个目标文本;regex_search查找目标文本匹配的任何部分。或者您可以将正则表达式更改为以“.*”结尾以吞下目标文本的其余部分。

于 2013-02-04T15:47:11.750 回答