#include <regex>
#include <string>
usingnamespace std::string_literals;
void usingRegExpressionsForParsingAString()
{
auto text{ R"(
#remove # to uncomment the following lines
timeout=120
server = 127.0.0.1
)"s};
auto pattern{ R"(^\s*(?!#)(\w+)\s*=\s*([\w\d]+)$)"s };
auto rx = std::regex{ pattern };
auto match = std::smatch{};
auto end = std::sregex_iterator{};
for ( auto it = std::sregex_iterator { std::begin(text), std::end(text), rx};
it != end; ++it)
{
std::cout << "'" << (*it)[1] << "'='" << (*it)[2] << "'" << std::endl;
}
}
If I remove the ! std::regex creation throws an exception; if I remove the ? it does not throw but no matches are found.
I tested it with another character and it does seem that (?!x) makes it impossible to match x character (for any character with no special meaning in the regular expression syntax...
@salem c, thanks for the link. regex searches have always looked rather arcane and mysterious, though admittedly I have not had any urgent need to bother learning it.