正则表达式练习——将文本中的日期格式化为标准格式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baiziyuandyufei/article/details/46575383
//匹配文本中的日期,并将日期转换为标准格式
void dateFormatEx(void)
{
    ifstream in(".\\chapter.23.4.1.in", ios::binary);
    if (!in)cerr << "no file\n";

    boost::regex pat("([\\d]+) ([\\w]+) ([\\d]+)");
    cout << "pattern: " << pat << '\n';

    int lineno = 0;
    string line;
    while (getline(in, line))
    {
        ++lineno;
        boost::smatch matches;
        if (boost::regex_search(line, matches, pat))
        {
            string day = matches[1];
            string mon = matches[2];
            string year = matches[3];
            line = boost::regex_replace(line, pat, year+"/"+mon+"/"+day);
        }
        cout << line << endl;
    }
}

输入语料
xxx

xxx

From: John Doe [email protected]
To: Mary Smith [email protected]
Subject: Saying Hello
Date: Fri, 21 Nov 1997 09:55:06 -0600
Message-ID: <[email protected]>

This is a message just to say hello.

So, “Hello”.

From: Joe Q. Public [email protected]
To: Mary Smith <@machine.tld:[email protected]>, , jdoe@test .example
Date: Tue, 1 Jul 2003 10:52:37 +0200
Message-ID: <[email protected]>

Hi everyone.

To: “Mary Smith: Personal Account” [email protected]
From: John Doe [email protected]
Subject: Re: Saying Hello
Date: Fri, 21 Nov 1997 11:00:00 -0600
Message-ID: [email protected]
In-Reply-To: <[email protected]>
References: <[email protected]> <[email protected]>

This is a reply to your reply.

----

输出结果
这里写图片描述

猜你喜欢

转载自blog.csdn.net/baiziyuandyufei/article/details/46575383