Boost logo

Boost Users :

From: John Maddock (john_at_[hidden])
Date: 2007-07-04 04:41:53


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 &regex)
> {
> 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.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net