Regex with backreference won't match in C++ -
i'm trying match 4 equal characters in c++.
these supposed match = kqqqq, zzzzq
this i've tried far:
std::string mano_to_reg = "kqqqq"; std::regex pokar("(.)\1{3}"); std::smatch match; std::cout << "match = " << std::regex_match(mano_to_reg, match, pokar) << "\n";
but won't match.
i've tried std::regex_search
, won't match either.
i've tried using basic , extend syntax
i've tried changing pattern "(.)\1{4}"
, "((.)\1{3})"
, every other combination of these.
i've tried matching other patterns other strings , of them work. seems problem backreference, i've looked everywhere , can't find why wont match.
i'm using clang++ 7.0.2 on os x 10.11.3 -std=c++11 -stdlib=libc++ flags.
i've tried g++ 5.3.0 -std=c++11 -std=gnu++11 flags.
you've got 2 problems:
- you need escape
\
. regex(.)\1{3}
correct, in order store in string literal, need escape it's"(.)\\1{3}"
. - you want
std::regex_search
instead ofstd::regex_match
. string"kqqqq"
not match regex(.)\1{3}
, substring"qqqq"
does.
the following program:
#include <iostream> #include <regex> #include <string> int main() { std::string mano_to_reg = "kqqqq"; std::regex pokar("(.)\\1{3}"); std::smatch match; std::cout << "match = " << std::regex_match(mano_to_reg, match, pokar) << "\n"; std::cout << "search = " << std::regex_search(mano_to_reg, match, pokar) << "\n"; }
match = 0 search = 1
Comments
Post a Comment