Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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/4/regex/18.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++_Regex - Fatal编程技术网

C++ 如何根据某个索引将字符串与正则表达式匹配?

C++ 如何根据某个索引将字符串与正则表达式匹配?,c++,regex,C++,Regex,我正在处理我的一个小项目,我需要将字符串与正则表达式值匹配,匹配的第一个字符需要精确地从某个索引开始 我需要在C++中这样做,但是找不到看起来像它会工作的方法或函数。 如果我把字符串堆栈溢出放入模式f.ow 如果索引为3,则应返回false。但如果我将索引设置为10,它应该返回true,并允许我找出与流实际匹配的内容。如果我输入一个11的索引,它也应该返回false 它通过构造正则表达式使用整个字符串 strRx = `"^[\\S\\s]{" + strOffset + "}(f.ow)";

我正在处理我的一个小项目,我需要将字符串与正则表达式值匹配,匹配的第一个字符需要精确地从某个索引开始

我需要在C++中这样做,但是找不到看起来像它会工作的方法或函数。

如果我把字符串堆栈溢出放入模式f.ow
如果索引为3,则应返回false。但如果我将索引设置为10,它应该返回true,并允许我找出与流实际匹配的内容。如果我输入一个11的索引,它也应该返回false

它通过构造正则表达式使用整个字符串

strRx = `"^[\\S\\s]{" + strOffset + "}(f.ow)";
// make a regex out of strRx
或者,使用迭代器跳转到要开始匹配的位置

bool FindInString( std::string& sOut, std::string& sInSrc, int offset )
{
    static std::regex Rx("(f.ow)");

    sOut = "";
    bool bRet = false;
    std::smatch _M;
    std::string::const_iterator _Mstart = sInSrc.begin() + offset;
    std::string::const_iterator _Mend   = sInSrc.end();

    if ( regex_search( _Mstart, _Mend, _M, Rx ) )
    {
        // Check that it matched at exactly the _Mstart position
        if ( _M[1].first == _Mstart )
        {
            sOut = std::string(_M[1].first, _M[1].second);
            bRet = true;
        }
    }
    return bRet;
}
如何根据某个索引将字符串与正则表达式匹配


将从索引开始的子字符串作为字符串参数传递,并将^添加到正则表达式的开头,使其仅在模式从子字符串的开头开始时匹配。

尝试此函数,希望它对您有用

#include<iostream>
#include<regex>
#include<string.h>
using namespace std;
bool exactRegexMatch(string str,int index){
    regex reg("f(.)ow");
    return regex_match(str.substr(index), reg);
}
int main(){
    if(exactRegexMatch("Stack Overflow",10)){
        cout<<"True"<<endl;
    }
    else{
        cout<<"False"<<endl;
    }
}

当您希望正则表达式在某个索引之后工作时,您可以从该索引开始创建一个新字符串。std::字符串s.begin+3,s.end;。idk,这是否是可能的。@Dimfred-您不需要创建新字符串,只需使迭代器=s.begin+3;等并在搜索函数中使用迭代器。但这样做也有问题,像锚和其他支持断言的东西一样。如果新的ECMAScript命中C++,可能会出现类似于正则表达式的查找。这看起来应该是有效的。@ SLN如果新ECMAScript命中C++,为什么会中断?如果新的ECMAScript命中C++,在RexEx中可能会有一个查找表?@ SLN,然后放弃使用A的想法。子字符串,并将^.{i}前置到正则表达式,其中i是索引。