0

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?

Jatin Ganhotra
  • 6,825
  • 6
  • 48
  • 71
  • 3
    More backslashes. You need to escape the backslash, so that it makes it through being interpreted by the c++ compiler, to be seen by the regex processor, so you end up with an actual square bracket, rather than a character range containing just `'0'`. i.e. `"Q\\[0\\]123"`. – BoBTFish Jul 22 '13 at 15:00
  • @BoBTFish: 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. What does that mean? – Jatin Ganhotra Jul 22 '13 at 18:25
  • 1
    Your debugger doesn't show regex strings, it shows C strings. In C, the single character '\' is *displayed* as '\\'. This is so your debugger can also show other 'escaped' characters such as '\t' and '\n'. It seems you get confused by C notation (double backslash) versus regexp notation. For a regexp that needs a single backslash, you need to insert *two* in your C code. And that's what the debugger shows as well. If you output your result with a standard printf or cout, you will see a single backslash where expected, because these routines do *not* escape special characters. – Jongware Jul 29 '13 at 01:19
  • @Jongware: Yes, I had got confused by the C notation and the regex notation. Figured it out later. Thanks :) – Jatin Ganhotra Jul 29 '13 at 05:43

2 Answers2

8

"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)".

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • 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. What does that mean? – Jatin Ganhotra Jul 22 '13 at 18:25
  • I understand that if I were to initialise actual_name with `"q\[0\]123"` it won't. The values are computed from different code and passed on for regex-matching. Please refer my previous comment and let me know what's wrong. – Jatin Ganhotra Jul 22 '13 at 18:27
0

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"

BRPocock
  • 13,638
  • 3
  • 31
  • 50