ede wd
ede wd

Reputation: 141

Cpp: Why the next regex match is not correct?

I have th next code sample:

    std::string str("example1   ");
    std::smatch sm;
    std::regex e("[a-zA-Z_]+[0-9a-zA-Z_]*\s*");
    if (std::regex_match(str, sm, e))
    {
        std::cout << "ok_match";
    }

It should accept everything including spaces, but it doesn't. For example, if the string will be:

std::string str("example1");

So "ok_match" will be print on the screen. Why is that?

Upvotes: 0

Views: 36

Answers (1)

mindriot
mindriot

Reputation: 5678

You have not escaped the "\s" sequence correctly. Actually, your compiler should be showing you a warning like

main.cpp: In function 'int main()':
main.cpp:9:16: warning: unknown escape sequence: '\s'
   std::regex e("[a-zA-Z_][0-9a-zA-Z_]*\s*");
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~

To represent a regex pattern such as \s in a C++ string, you will need to escape the backslash in order to get a literal backslash in your string. To elaborate a bit:

  • "\n" represents a linebreak. You've probably seen that before.
  • "\\n" represents a backslash, followed by the letter n.
  • In the same vein, "\s" is treated as an escape sequence by the compiler, except the sequence "\s" does not actually exist.
  • You want an actual backslash +s in your string, so you need to write "\\s": a backslash, followed by the letter s. This, in turn, is understood by std::regex to be a shorthand for whitespace.

This program should do what you are looking for:

#include <regex>
#include <string>
#include <iostream>

int main()
{
  std::string str("example1   ");
  std::smatch sm;
  std::regex e("[a-zA-Z_][0-9a-zA-Z_]*\\s*");
  if (std::regex_match(str, sm, e))
  {
    std::cout << "ok_match";
  }
}

Live on coliru

Upvotes: 1

Related Questions