Вопрос

I was playing around with Boost::regex and this is the first time I'm working with regex as well as Boost, so forgive me if the question is really stupid.
I am getting a NO_MATCH with the following values:

actual_name = "q\[0\]123"
user_name   = "q[0]123"

P.S. In the debugger, when I printed actual_name it showed - q\\[0\\]123. But, when I did actual_name.size(), it came out to be 9.

Here is my code:

boost::regex regexpr( actual_name );
boost::match_results<pstring::const_iterator> what;
boost::regex_match(user_name, what, regexpr);

if(what[0].matched)
{
     // Match found
}
else
{
    // NO_match found
}

I tried the same combination of regular_expression = "q\[0\]123" and test-string = "q[0]123" on Rubular.com and it returns a Complete_Match there.

What am I missing?

Это было полезно?

Решение

"q\[0\]123" compiles?

\[ is not a backslash character followed by an opening square bracket character. It's an escape sequence. I don't remember it being a valid escape sequence, but it might be an extension in your compiler.

You need to escape the backslashes like "q\\[0\\]123", or use a C++11 raw string literal like R"(q\[0\]123)".

Другие советы

If that's your actual assignment code to actual_name, it seems you're not doubling up your \\ to protect them from the C++ compiler: "q\\[0\\]123"

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top