2012-09-03 19 views
0

我想使用命名的子表達式來使用boost :: regex_replace和命名的子表達式來替換輸入字符串中的多個模式。在Boost正則表達式中命名爲Subexpressions錯誤

我使用下面的代碼:

std::string s="Sun Fun Gun Tic Tac Toe ;$!"; 
boost::regex expr("(?<from1>.un)|(?<from2>[:;!])"); //Throws the error 
std::string fmt("(?'from1'nm)(?'from2'sp)"); 
std::string s2 = boost::regex_replace(s,expr,fmt,boost::match_default|boost::format_all); 
std::cout<<s2<<std::endl; 

但是,當運行茨艾倫引發以下錯誤:

terminate called after throwing an instance of 'boost::regex_error' 
what(): Invalid preceding regular expression 
Aborted 

請指導,以什麼我可能做錯了什麼?

+0

版本的提升? http://liveworkspace.org/code/5fe67c46a9325236ff224de62c944198 – ForEveR

+0

我正在使用Boost 1.45 – Mindstorm

回答

0

不知道Boost,但基於Boost的std::regex(在TR1和C++ 11中)不支持命名的子表達式。

+0

以下頁面描述了對Boost庫中Perl Regex語法的支持:[link](http://www.boost.org/doc/libs /1_40_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html)。 所以,我相信它也支持相同。 – Mindstorm

+0

@Mindstorm - 感謝您的鏈接。他們在標準採納後改變了它(標準不做perl;默認是ECMAScript,它不是「perl」的同義詞)。 –

+0

@Mindstorm - 這不會影響標記爲「引發錯誤」的行,但它看起來像格式字符串應該是''+ + {from1} nm $ + {from2} sp「'。也就是說,'$ + {NAME}'「[o]輸出與名爲'NAME'的子表達式匹配的任何東西。 –