我試圖按照這裏的例子:boost正則表達式不匹配?
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(「^ \\\ [(。+)\\\]」); – tgmath