Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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++ 如何使用C++;11还是增加?_C++_Regex_Boost_C++11 - Fatal编程技术网

C++ 如何使用C++;11还是增加?

C++ 如何使用C++;11还是增加?,c++,regex,boost,c++11,C++,Regex,Boost,C++11,是否有一个库函数可用于获取第一个正则表达式匹配项的长度(如果没有匹配项,则返回0) 所以我需要一些类似的函数原理: int length_of_match(const std::string& s, const std::string& rx) { ... } 例如,可通过以下方式轻松使用: int a = length_of_match("number = 10", "^[a-zA-z][a-zA-z0-9]*"); int b = length_of_match("

是否有一个库函数可用于获取第一个正则表达式匹配项的长度(如果没有匹配项,则返回0)

所以我需要一些类似的函数原理:

int length_of_match(const std::string& s, const std::string& rx)
{
    ...
}
例如,可通过以下方式轻松使用:

int a = length_of_match("number = 10", "^[a-zA-z][a-zA-z0-9]*");
int b = length_of_match(" = 10", "^[a-zA-z][a-zA-z0-9]*");
然后
a==6
b==0

?

编辑:

谢谢你的回复,这对我帮助很大。我的问题的解决办法是:

int length_of_match(const std::string& s, const std::string& rx)
{
    boost::regex expr(rx);
    boost::match_results<std::string::const_iterator> what;
    if (!regex_search(s.begin(), s.end(), what, expr)) return 0;
    return what.length();
}
匹配的整数长度(常数std::string&s,常数std::string&rx) { boost::正则表达式(rx); 匹配结果是什么; 如果(!regex_search(s.begin(),s.end(),what,expr))返回0; 返回what.length(); } Boost工作得很好。我也尝试过STL正则表达式,但我发现在mingw GCC 4.7.1中STL正则表达式的功能有问题:

在boost文档中,有:
length(n)
返回指定匹配的长度


另一种方法是:获取匹配的
字符串
并返回其长度。

您可以获得
std::regex_match的结果匹配长度
如果您需要,几乎所有regex搜索工具都会为您提供匹配的子字符串(例如,对于存在的匹配,不是“是/否”)。然后,您可以询问该字符串的长度。