繁体   English   中英

Boost :: Regex在C ++中的用法

[英]Boost::Regex usage in C++

我有这个正则表达式,我想问一下我是否正确使用了它

regex re("(\\S+)=[\"']?((?:.(?![\"']?\\s+(?:\\S+)=|[>\"']))+.)[\"']?",regex::extended,regex::icase);
       while (getline(input, temp, '>')) {
            parse+=temp;
            bool isMatchFound = regex_match(parse, match, re);

            if(isMatchFound){
            //Do something
            }
        }

它应与<a href="bla.html"></a>标记match[2]match[2]应为bla.html

它引发以下异常:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::regex_error> >'
  what():  Invalid preceding regular expression prior to repetition operator.  The error occurred while parsing the regular expression fragment: 'ed_state**>>>HERE>>>, boost::r

我认为扩展不会削减其断言。 最好使用Perl模式。
我只是用您的选项测试了您的正则表达式,对我来说也是如此。

我将使用这些定义之一(特别是MOD或MODx)。
我使用了MODxs,它在启动时没有引发错误。

 #define MOD    regex_constants::perl | boost::regex::no_mod_s | boost::regex::no_mod_m 
 #define MODx   regex_constants::perl | boost::regex::no_mod_s | boost::regex::no_mod_m | regex_constants::mod_x
 #define MODs   regex_constants::perl | boost::regex::no_mod_m | regex_constants::mod_s
 #define MODxs  regex_constants::perl | boost::regex::no_mod_m | regex_constants::mod_s | regex_constants::mod_x

 #define MODm   regex_constants::perl | boost::regex::no_mod_s  
 #define MODxm  regex_constants::perl | boost::regex::no_mod_s| regex_constants::mod_x
 #define MODsm  regex_constants::perl | regex_constants::mod_s
 #define MODxsm regex_constants::perl | regex_constants::mod_s | regex_constants::mod_x


 boost::regex  rx( "(\\S+)=[\"']?((?:.(?![\"']?\\s+(?:\\S+)=|[>\"']))+.)[\"']?", MODxs );   // or MODs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM