Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.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_C++11_Match - Fatal编程技术网

C++ 确定C+的位置+;11个正则表达式匹配

C++ 确定C+的位置+;11个正则表达式匹配,c++,regex,c++11,match,C++,Regex,C++11,Match,如何有效地确定捕获组在搜索字符串中的位置?获得整场比赛的位置很容易,但除了第一场比赛外,我看不到任何明显的方法来抓捕小组 这是一个简化的示例,假设“a*”和“b*”是复杂的正则表达式,运行起来很昂贵 #include <iostream> #include <regex> #include <string> using namespace std; int main() { regex matcher("a*(needle)b*");

如何有效地确定捕获组在搜索字符串中的位置?获得整场比赛的位置很容易,但除了第一场比赛外,我看不到任何明显的方法来抓捕小组

这是一个简化的示例,假设“a*”和“b*”是复杂的正则表达式,运行起来很昂贵

#include <iostream>
#include <regex>
#include <string>
using namespace std;

int main()   
{
    regex matcher("a*(needle)b*");
    smatch findings;
    string haystack("aaaaaaaaneedlebbbbbbbbbbbbbb");

    if( regex_match(haystack, findings, matcher) )
    {
        // What do I put here to know how the offset of "needle" in the 
        // string haystack?

        // This is the position of the entire, which is
        // always 0 with regex_match, with regex_search
        cout << "smatch::position - " << findings.position() << endl;

        // Is this just a string or what? Are there member functions
        // That can be called?
        cout << "Needle - " << findings[1] << endl;
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
正则表达式匹配器(“a*(指针)b*”;
smatch结果;
线状草堆(“aaaaaaaaa针线草堆”);
if(正则表达式匹配(草堆、结果、匹配器))
{
//我在这里放什么来知道“针”在
//干草堆?
//这是整个的位置,也就是
//始终使用正则表达式匹配0,使用正则表达式搜索

cout直到72小时过去,没有更好的答案出现之前,我不会将此标记为并回答

在问这个问题之前,我假设smatch::position没有我关心的参数,因为当我阅读页面时,“sub”参数显然不是匹配容器中的索引。我认为它与“sub”字符串和整个匹配的偏移量值有关

因此,我的答案是:

cout << "Needle Position- " << findings.position(1) << endl;
cout根据,您可以通过
match[n]访问指向捕获文本开头和结尾的迭代器。首先
match[n]。其次
。要获取开始和结束索引,只需使用
haystack.begin()执行指针算术即可

if(结果[1]。匹配){

如果没有您的帮助,我无法找到sub_match文档,即使这正是我要查找的。我现在知道您一定是从regex_match参数转到了,然后从模板参数转到了页面。现在看起来很明显,但您问了一个问题:“这个东西被[]操作符返回的原因是什么?”这就是我需要问的。
if (findings[1].matched) {
    cout << "[" << findings[1].first - haystack.begin() << "-"
                << findings[1].second - haystack.begin() << "] "
                << findings[1] << endl;
}