|
Boost Users : |
From: John Maddock (john_at_[hidden])
Date: 2004-09-13 09:22:01
> It seems to consistently fail. This same example works in both Java and
> Perl, and with the PCRE C++ regex library. No exception is being caught
> either.
>
> I posted the actual code to show that i don't think i'm doing anything
> particularly unique - i call regex_match (though even regex_search fails
> to match). I even tried formatting a file for the regression test
> harness with this example, and it confirmed that this particular example
> does not succeed.
>
> Can someone please provide suggestions regarding:
> a) what is going wrong
> b) how to fix it
>
> It should be noted that i'm running linux, boost version 1.31.0, with
> the regex patch installed.
It works for me, *only* if you use regex_search (see code below), the reason
that you need to use regex_search is that the match found is only a prefix
of the string, and regex_match will only succeed if the *whole* of the
string is matched.
John.
And here's the code:
#include <string>
#include <iostream>
#include <iomanip>
#include <boost/regex.hpp>
int main()
{
bool result = false;
std::string str = "orange and ba.ana and kiwi";
std::string exp(".([a-z]+).*[ \t\n].*\\.([a-z]+)");
try {
boost::smatch what;
boost::regex e(exp);
// use default format flags
result = regex_search(str, what, e);
if (result)
{
std::cout << what[0] << std::endl << what[1] << std::endl << what[2] <<
std::endl;
}
} catch (std::runtime_error &e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
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