Re: [Boost-users] Mismatch and regex newbie problem
For example, If the regex to search for is "test" and the string is "tast" , then 1 mismatch is found position 2. John, i think the code below you have submitted is doing exactly what i want right ?? std::string::const_iterator start = in.begin(); std::string::const_iterator end = in.end(); boost::sregex_token_iterator i(start, end, *regex, -1), j; while(i != j) { cout << *i << endl; }
Hello, I'm using the boost regex library to search short expressions in large strings. The code below works fine but it almost does what ever i want. The thing i'm missing and that i'm not sure to handle are the mismatches. Basically when i look for a regex in a string i want to have the possibility to get the number of mismatches and their respective positions. I read through the docs and saw that the boost_check may do the work. Well the think is that i'm not sure how to use it with my code and since the boost library is quite complex it would be great if somebody could help to move forward.
How can i use the code below to get the mismatches and their positions with boost_check ???
What's boost_check and what's it got to do with regex?
#####Setting the regex ..... re = "test"; boost::regex *regex = new boost::regex(re,boost::regbase::icase); .....
#######Getting the matched ....... std::string::const_iterator start = in.begin(); std::string::const_iterator end = in.end(); boost::smatch match; while (boost::regex_search(start,end,match,*regex,boost::match_extra)) { cout << match.[0] << endl; start = match[0].second; }
#############Getting the mismatches ??? How ???
Any help would be extemely helpful !!!
What do you mean by mismatches? The sections of the string that didn't match? That would be: std::string::const_iterator start = in.begin(); std::string::const_iterator end = in.end(); boost::smatch match; while(boost::regex_search(start,end,match,*regex)) { cout << match.prefix() << endl; start = match[0].second; } // avoid a fencepost error: cout << match.suffix() << endl; Note that the match_extra flag is unnecessary unless you really need to record repeated captures. Normal capture information - of the kind you get from Perl and other regex engines don't need this - and it slows down matching as well as requiring building the lib in a non-default mode. You can also do this rather easier with regex_token_iterator: std::string::const_iterator start = in.begin(); std::string::const_iterator end = in.end(); boost::sregex_token_iterator i(start, end, *regex, -1), j; while(i != j) { cout << *i << endl; } HTH, John.
participants (1)
-
david v