Question

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;
}
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top