我正在尝试按照此处的示例进行操作:
http://www.boost.org/doc/libs/1_31_0/libs/regex/doc/syntax.html
我想匹配这种形式的行:
[ foo77 ]
这应该很简单,我尝试了这样的代码片段:
boost::regex rx("^\[ (.+) \]");
boost::cmatch what;
if (boost::regex_match(line.c_str(), what, rx)) std::cout << line << std::endl;
但我不匹配那些行。我尝试了以下变体表达式:
"^\[[:space:]+(.+)[:space:]+\]$" //matches nothing
"^\[[:space:]+(.+)[:space:]+\]$" //matches other lines but not the ones I want.
我做错了什么?
最佳答案
将 boost::regex rx("^\[ (.+)\]");
更改为 boost::regex rx("^\\[ (.+)\\]");
,它会正常工作,编译器应该警告无法识别的字符转义序列。
关于c++ - boost 正则表达式不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21756496/