Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 需要解析一个字符串,有一个掩码(类似于“yr-%mh-%dy”这样的东西),所以我得到int值_C++_String_Datetime_Parsing_Tags - Fatal编程技术网

C++ 需要解析一个字符串,有一个掩码(类似于“yr-%mh-%dy”这样的东西),所以我得到int值

C++ 需要解析一个字符串,有一个掩码(类似于“yr-%mh-%dy”这样的东西),所以我得到int值,c++,string,datetime,parsing,tags,C++,String,Datetime,Parsing,Tags,例如,我必须在字符串“日期为2009-8-25”中以标题中提到的格式查找时间(但%-标记顺序可能不同)。”我如何让程序解释标签,以及使用什么结构更好地存储标签,以及如何处理某些日期字符串的信息?我会将标签字符串转换为正则表达式,捕获3个字段并搜索它。正则表达式的复杂性将取决于您希望为%yr接受的内容。您也可以使用一个不太严格的表达式,然后检查有效值,这可能会导致更好的错误消息(“无效月份:Augsut”而不是“未找到日期”)或误报,具体取决于上下文。首先查看库。它可能是你想要的,但我发现缺乏搜索

例如,我必须在字符串
“日期为2009-8-25”中以标题中提到的格式查找时间(但
%
-标记顺序可能不同)。”
我如何让程序解释标签,以及使用什么结构更好地存储标签,以及如何处理某些日期字符串的信息?

我会将标签字符串转换为正则表达式,捕获3个字段并搜索它。正则表达式的复杂性将取决于您希望为%yr接受的内容。您也可以使用一个不太严格的表达式,然后检查有效值,这可能会导致更好的错误消息(“无效月份:Augsut”而不是“未找到日期”)或误报,具体取决于上下文。

首先查看库。它可能是你想要的,但我发现缺乏搜索

要进行您需要的自定义日期搜索。它包含你需要的任何东西。让我们看看我匆忙写的例子。首先,您应该解析您的自定义模式,使用Xpressive很容易。首先查看您需要的标题:

#include <string>
#include <iostream>
#include <map>
#include <boost/xpressive/xpressive_static.hpp>
#include <boost/xpressive/regex_actions.hpp>

//make example shorter but less clear
using namespace boost::xpressive;
更多信息和说明请参见和。 现在让我们分析一些模式:

  std::string pattern("%yr-%mh-%dy"); // this will be parsed

  sregex_token_iterator begin( pattern.begin(), pattern.end(), rx ), end;
  if(begin == end) throw std::runtime_error("The pattern is empty!");
将在我们的令牌上迭代,每次它都将设置tag_id变量。我们所要做的就是使用这些令牌构建正则表达式。我们将使用数组中定义的静态正则表达式的相应部分来构造此正则表达式:

sregex regex_group[] = {
    range('1','9') >> repeat<3,3>( _d ), // 4 digit year
    as_xpr( "January" ) | "February" | "August", // not all month XD so lazy
    repeat<2,2>( range('0','9') )[    // two digit day
    check(as<int>(_) >= 1 && as<int>(_) <= 31) ], //only bettwen 1 and 31
    as_xpr( '%' )  // match escaped %
};
接下来,我们将从开始到结束进行迭代,并附加下一个正则表达式:

while(++begin != end)
{
    if(tag_id>=0)
    {
        sregex nextregex = custom_regex >> regex_group[tag_id];
        custom_regex = nextregex;
    }
    else
    {
        sregex nextregex = custom_regex >> as_xpr(begin->str());
        custom_regex = nextregex;
    }
}
现在我们的正则表达式准备好了,让我们查找一些日期:-]

std::string input = "The date is 2009-August-25.";

smatch mydate;
if( regex_search( input, mydate, custom_regex ) )
    std::cout << "Found " << mydate.str() << "." << std::endl;
std::string input=“日期是2009年8月25日。”;
smatch-mydate;
if(正则表达式搜索(输入、mydate、自定义正则表达式))
标准::cout
sregex custom_regex = (tag_id>=0) ? regex_group[tag_id] : as_xpr(begin->str());
while(++begin != end)
{
    if(tag_id>=0)
    {
        sregex nextregex = custom_regex >> regex_group[tag_id];
        custom_regex = nextregex;
    }
    else
    {
        sregex nextregex = custom_regex >> as_xpr(begin->str());
        custom_regex = nextregex;
    }
}
std::string input = "The date is 2009-August-25.";

smatch mydate;
if( regex_search( input, mydate, custom_regex ) )
    std::cout << "Found " << mydate.str() << "." << std::endl;