Hello, I'm trying to use regex_search to look for regular expressions. regex = (david|divad) stringtosearch = "david" So I'm looking for regex above in the string "david". As you see a very basic example. I'm expecting to find two hits, but I thought I should recover "david" and "david" and not twice the same match "david" ??? Here is a snippet of the code: .... boost::smatch match; start = stringtosearch.begin(); //that would be the start of "david" end = stringtosearch.end(); // that would be the end of "david" old_pos = 0; cout << "Looking for regex:" << regex << " in string " << stringtosearch << endl; while (boost::regex_search(start,end,match,regex)) { old_pos += match.position()+1; std::string t = match[0]; std::cout << "\tFound:" << match[0] << endl; old_pos += match.length(); start = match[0].second; } catch(const std::exception ®ex) { std::cerr << "Exception:" << regex.what() << std::endl; exit(-1); } Here is the output: Looking for regex:(david|divad) in string david Found david Looking for regex:(david|divad) in string david Found david As you can see, pattern "david" is found twice ??? I was expecting two different patterns to be found "david" and "divad". By the way how can I recover the total number of matches found ?? thanks, david _________________________________________________________________ MSN Messenger : discutez en direct avec vos amis ! http://www.msn.fr/msger/default.asp
david v wrote:
Hello, I'm trying to use regex_search to look for regular expressions.
regex = (david|divad) stringtosearch = "david"
So I'm looking for regex above in the string "david". As you see a very basic example. I'm expecting to find two hits, but I thought I should recover "david" and "david" and not twice the same match "david" ???
Here is a snippet of the code:
.... boost::smatch match; start = stringtosearch.begin(); //that would be the start of "david" end = stringtosearch.end(); // that would be the end of "david" old_pos = 0; cout << "Looking for regex:" << regex << " in string " << stringtosearch << endl; while (boost::regex_search(start,end,match,regex)) {
old_pos += match.position()+1; std::string t = match[0];
std::cout << "\tFound:" << match[0] << endl;
old_pos += match.length(); start = match[0].second;
} catch(const std::exception ®ex) { std::cerr << "Exception:" << regex.what() << std::endl; exit(-1); }
Here is the output: Looking for regex:(david|divad) in string david Found david Looking for regex:(david|divad) in string david Found david
As you can see, pattern "david" is found twice ??? I was expecting two different patterns to be found "david" and "divad".
Um, because the stringtosearch contains "divad" both times the code is called?
cout << "Looking for regex:" << regex << " in string " << stringtosearch << endl;
Outputs
Looking for regex:(david|divad) in string david
Both times.
By the way how can I recover the total number of matches found ??
By keeping count? John.
participants (2)
-
david v
-
John Maddock