regex_search() return false for positive lookbehind and look

regex_search() return false for positive lookbehind and lookahead

本文关键字:positive for lookbehind and look false return search regex      更新时间:2023-10-16

我试图在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上测试了这一正则表,它告诉我应该找到字符串" = hellowsettingsworld"

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

但是std::regex_search()总是返回false?

不确定我是否正在使用regex_search功能错误或我的正则表达式是否有问题?

c <regex>根据ecmascript(又称JavaScript)RegExp实现,带有较小的扩展名来支持POSIX字符类。由于ecmascript RegExp不支持look-behind,因此C <regex>也不支持语法。

正如Casimir et hippolyte所建议的,如果您想要更多的花哨的正则表达式功能,请使用Boost Library。