[英]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.