regex_search()返回false表示正向lookbehind和lookahead

时间:2015-05-15 01:27:27

标签: c++ regex c++11

我试图在C ++ 11中使用regex_search函数,而我的代码找不到我期望的字符串。

    std::string regexString = "(?<=settings1)(.*)(?=;)";
    std::regex rgx(regexString);
    std::smatch match;
    std::string settingValue;


    if (std::regex_search("setting1=hellowSettingsWorld;", match, rgx)){
        // match fond
    settingValue = match[1];
        cout << "string found "  << settingValue << endl;
    }else{
    cout << "string not found " 
    }

我在regex101上测试了这个正则表达式,它告诉我它应该找到字符串&#34; = hellowSettingsWorld&#34;

https://regex101.com/r/nU7qK5/1

然而std::regex_search()总是返回false?

我不确定我是否错误地使用了regex_search函数,或者我的正则表达式是否有问题?

1 个答案:

答案 0 :(得分:0)

C ++ <regex>根据ECMAScript(a.k.a. JavaScript)RegExp实现,扩展名小,支持POSIX字符类。由于ECMAScript RegExp不支持look-behind,因此C ++ <regex>也不支持语法。

Casimir et Hippolyte建议的那样,如果您想要更多精美的正则表达式功能,请使用Boost库。