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

//匹配文本中的日期,并将日期转换为标准格式
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 jdoe@machine.example
To: Mary Smith mary@example.net
Subject: Saying Hello
Date: Fri, 21 Nov 1997 09:55:06 -0600
Message-ID: <1234@local.machine.example>

This is a message just to say hello.

So, “Hello”.

From: Joe Q. Public john.q.public@example.com
To: Mary Smith <@machine.tld:mary@example.net>, , jdoe@test .example
Date: Tue, 1 Jul 2003 10:52:37 +0200
Message-ID: <5678.21-Nov-1997@example.com>

Hi everyone.

To: “Mary Smith: Personal Account” smith@home.example
From: John Doe jdoe@machine.example
Subject: Re: Saying Hello
Date: Fri, 21 Nov 1997 11:00:00 -0600
Message-ID: abcd.1234@local.machine.tld
In-Reply-To: <3456@example.net>
References: <1234@local.machine.example> <3456@example.net>

This is a reply to your reply.

----

输出结果
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值