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

Boost::Regex usage in C++

本文关键字:C++ Regex Boost      更新时间:2023-10-16

我得到了这个正则表达式,我想问我是否正确使用了它

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]应该是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

我不认为extended会因为断言而削减它。最好使用Perl模式
我刚刚用你的选项测试了你的正则表达式,它对我来说也是一样的。

我会使用其中一个定义(特别是MOD或MODxs)
我使用了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