문제

In the re2 header it says

// C++ interface to the re2 regular-expression library.
// RE2 supports Perl-style regular expressions (with extensions like
// \d, \w, \s, ...).

I notice my pattern failing then notice \w doesn't seem to work. here is my code. Why isn't it working?

#include <re2/re2.h>
#include <iostream>

int main(){
    RE2::Arg s, i;
    std::string sz("a or b");
    RE2::Replace(&sz, "\w", "!");
    std::cout << sz << std::endl;
}
도움이 되었습니까?

해결책

As Johnny Mopp mentioned in his comment, you are passing the literal \w into the string, when you probably meant to pass in the expression \w. Thus, you need to use \\w to pass the expression.

In fact, in the header that you linked yourself, the examples all have \\w in the strings.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top