Mismatch and regex newbie problem
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 ??? #####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 !!! david
david v wrote:
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.
# danova_fr@hotmail.com / 2006-08-29 09:21:59 +0000:
How can i use the code below to get the mismatches and their positions with boost_check ???
#####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 ???
You want to subtract the matches from the input? -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991
participants (3)
-
david v
-
John Maddock
-
Roman Neuhauser