-1

I am attempting to use the regex_search function in C++11 and my code is not finding the string as I would expect.

    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 " 
    }

I tested this regex out on regex101 and it tells me it should find the string "=hellowSettingsWorld"

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

however the std::regex_search() always returns false?

not sure if i am using the regex_search function incorrectly or if there is something wrong with my regular expression?

Denilson Amorim
  • 9,642
  • 6
  • 27
  • 31
Matt
  • 9
  • 1
  • 3
  • 3
    simply use: `settings1(.*)(?=;)` or `settings1(.*);`, the lookbehind is not available in ECMAscript regex. (with regex101, you should use the javascript mode for C++). If you really need a more advanced regex engine, use libboost. – Casimir et Hippolyte May 15 '15 at 01:34
  • thanks! appreciate the response, but it looks like neither of those suggestions worked. the regex_search function is still returning false. all though i do see that when I switched to javascript in regex101 why they are correct.... – Matt May 15 '15 at 01:54
  • 1
    @Matt The string you're trying to match contains `setting1` but your pattern has `settings1` instead. – Chris Smeele May 15 '15 at 02:24
  • @Matt: When you switch to JavaScript on regex101, note that the status is ERROR. – nhahtdh May 15 '15 at 03:02

1 Answers1

1

C++ <regex> implements according to ECMAScript (a.k.a. JavaScript) RegExp with a small extension to support POSIX character classes. Since ECMAScript RegExp doesn't support look-behind, C++ <regex> also doesn't support the syntax.

As Casimir et Hippolyte suggested, use Boost library if you want more fancy regex features.

Community
  • 1
  • 1
nhahtdh
  • 55,989
  • 15
  • 126
  • 162