该表达式包含不匹配的括号

时间:2015-03-12 04:34:05

标签: c++ regex

我的程序给了我一条错误消息:libc++abi.dylib: terminating with uncaught exception of type std::__1::regex_error: The expression contained mismatched ( and )我无法找到解决方法。括号是什么?

#include <regex>
#include <iostream>
#include <string>
using namespace std;
using namespace regex_constants;

int main() {
  // here can be url, or hashtag or split string: 
  // www.example.com or #followback or currentratesoughttogodown and output must be example or followback or currentratesoughttogodown.
  string input = "www.example.com"; 
  smatch m;
  regex_match ( input, m, regex ("(?<=www.|\\#)(\\w+)(?=\\.)?") );

  return 0;
}

我使用gcc编译:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix

1 个答案:

答案 0 :(得分:0)

  

C ++ 11使用ECMAScript的(ECMA-262)正则表达式语法,因此它不具有后瞻性(C ++ 11支持的其他版本的正则表达式也没有后悔)。

Lookahead即支持(?=)

您可以尝试(?:www\\.|\\#)(\\w+)(?=\\.)

然后抓住第1组。或者你可以使用支持Boost.Regex的{​​{1}}或Boost.Xpressive进行调查。