Re: [Boost-users] Simple regex question

Ok i understand david and divad is the same so it's normal to find it twice, however why it always return as match "David" and not both matches "david" and "divad" in the order they appear in the searched string??? does not make sense to me ?? How can i get the matches in the same order they appear in the string ?? 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. _________________________________________________________________ Testez Windows Live Mail Beta ! http://www.ideas.live.com/

david v wrote:
Ok i understand david and divad is the same so it's normal to find it twice, however why it always return as match "David" and not both matches "david" and "divad" in the order they appear in the searched string??? does not make sense to me ?? How can i get the matches in the same order they appear in the string ??
You missed my point: according to your own program output they are *not* both present in the string being searched. John.
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.
_________________________________________________________________ Testez Windows Live Mail Beta ! http://www.ideas.live.com/
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On 7/4/07, david v
Ok i understand david and divad is the same so it's normal to find it twice, however why it always return as match "David" and not both matches "david" and "divad" in the order they appear in the searched string??? does not make sense to me ?? How can i get the matches in the same order they appear in the string ??
match returns what part of the input string matched the regex, not what part of the regex matched the input string. so regex = "f*.r" stringtosearch = "foobarblah" match is "foobar" not "f*.r" Is that the confusion here? Tony
participants (3)
-
david v
-
Gottlob Frege
-
John Maddock