mercredi 16 septembre 2020

Make dollar and caret only match at beginning/end of string, not before/after embedded newlines

This little code which follows outputs

<hello>
<world>

demonstrating that ^ and $ are also matching after and before of \n respectively. How can I change this behavior, and have them only match at beginning and end of string? (In this case, there would be no match in the example str input.)

#include <boost/regex.hpp>
#include <iostream>
int main() {
    std::string tokenRegex = "^[^\n\r]+$";
    std::string str = "hello\nworld";
    boost::sregex_iterator rit{std::begin(str), std::end(str), boost::regex{tokenRegex}};
    boost::sregex_iterator end;

    while (rit != end) {
        std::cout << '<' << rit->str() << '>' << '\n';
        ++rit;
    }
}

Aucun commentaire:

Enregistrer un commentaire