我有一个文本,其日期可以像这样:2011-02-02
或者像这样:02/02/2011
,这是我迄今为止写的,我的问题是,如果有一个很好的将这两个正则表达式合并为一个的方法?结合两个正则表达式C++ 0x
std::regex reg1("(\\d{4})-(\\d{2})-(\\d{2})");
std::regex reg2("(\\d{2})/(\\d{2})/(\\d{4})");
smatch match;
if(std::regex_search(item, match, reg1))
{
Date.wYear = atoi(match[1].str().c_str());
Date.wMonth = atoi(match[2].str().c_str());
Date.wDay = atoi(match[3].str().c_str());
}
else if(std::regex_search(item, match, reg2))
{
Date.wYear = atoi(match[3].str().c_str());
Date.wMonth = atoi(match[2].str().c_str());
Date.wDay = atoi(match[1].str().c_str());
}
一些正则表达式的语法已命名组。 PCRE可以,但我认为它不支持重名。也许问题是:C++ 0x上的任何正则表达式变体是否支持重复的命名组? – 2011-04-28 08:14:57
我没有看到好处。这比组合的正则表达式更容易阅读。这有利于维护,这是不容低估的一个因素。这个代码甚至可以在没有文档的情况下阅读 - 结合它,你需要记录“怪物 - 正则表达式”。 – 2011-04-28 08:25:41