正则表达式练习——将文本中的日期格式化为标准格式
//匹配文本中的日期,并将日期转换为标准格式
void dateFormatEx(void)
{
ifstream in(".chapter.23.4.1.in",ios::binary);
if (!in)cerr << "no filen";
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;
}
}
文本语料 xxxFrom: John Doe jdoe@machine.example This is a message just to say hello. So,“Hello”.From: Joe Q. Public john.q.public@example.com Hi everyone.To: “Mary Smith: Personal Account” smith@home.example This is a reply to your reply.---- 输出结果 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |