c++ regex convert regex to c++ code -
first time regex (in c++ is)
i have hard time writing
(?<=name=")(?:[^\\"]+|\\.)*(?=")
that matches example name="blabla" xyz blabla code... how i
std::regex thename("(?<=name=")(?:[^\\"]+|\\.)*(?=")");
correctly please?
you need use capturing rather positive lookbehind in c++ regex. also, advisable use unroll-the-loop principle unroll ([^"\\]|\\.)*
subpattern make regex fast can be, see [^\"\\]*(?:\\.[^\"\\]*)*
. also, advisable use raw string literals (see r"(<pattern>)"
) when defining regex patterns in order avoid overescaping.
see c++ demo:
#include <iostream> #include <regex> using namespace std; int main() { std::string s = "name=\"bla \\\"bla\\\"\""; std::regex thename(r"(name=\"([^\"\\]*(?:\\.[^\"\\]*)*)\")"); std::smatch m; if (regex_search(s, m, thename)) { std::cout << m[1].str() << std::endl; } return 0; }
result: bla \"bla\"
Comments
Post a Comment