user1927896
user1927896

Reputation: 11

Why does "[\\s\\S]" not working in C++11's regex

I have the code below this:

#include <string>
#include <regex>

int main(int argc, char const *argv[]) {
  std::string s = "_apple_";

  std::regex r1("_(\\s|\\S)+_");
  std::regex r2("_[\\s\\S]+_");
  std::regex r3("_.+_");
  std::regex r4("_[pale]+_");

  std::smatch sm;
  printf("r1:%d r2:%d r3:%d r4:%d\n", 
        std::regex_match(s, sm, r1), 
        std::regex_match(s, sm, r2), 
        std::regex_match(s, sm, r3), 
        std::regex_match(s, sm, r4));

  return 0;
}

output:r1:1 r2:0 r3:1 r4:1

I can not understand why r2 is not match?

My environment is:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 10.0.0 (clang-1000.11.45.5) Target: x86_64-apple-darwin17.7.0 Thread model: posix InstalledDir:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin.

Upvotes: 1

Views: 308

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627128

The clang regex flavor is POSIX ERE acc. to clang-format regex syntax reference. In POSIX bracket expressions, the usual regex escape sequences, like \s, \d, \w, and even \], are not supported.

The [\s\S] is the same as [\\sS], and matches a backslash, s and S chars.

However, in POSIX regex standard, . matches any chars including line break chars thus there is no need using [\s\S] workaround.

Upvotes: 0

Related Questions