[英]Boost::Regex usage in C++
I got this regex and I'd like to ask if I am using it correctly 我有这个正则表达式,我想问一下我是否正确使用了它
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
}
}
It should match <a href="bla.html"></a>
tags and match[2]
should be bla.html
它应与
<a href="bla.html"></a>
标记match[2]
而match[2]
应为bla.html
It throws this exception: 它引发以下异常:
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
I don't think extended is going to cut it for assertions. 我认为扩展不会削减其断言。 Best to use Perl mode.
最好使用Perl模式。
I just tested your regex with your options, it does the same for me. 我只是用您的选项测试了您的正则表达式,对我来说也是如此。
I would use one of these defines (specifically MODs or MODxs). 我将使用这些定义之一(特别是MOD或MODx)。
I used MODxs and it didn't throw an error on startup. 我使用了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.