Boost logo

Boost Users :

From: Caleb Epstein (caleb.epstein_at_[hidden])
Date: 2004-09-13 08:27:47


On Sun, 12 Sep 2004 12:53:56 -0400 (EDT), Andrew Dennison
<adenniso_at_[hidden]> wrote:
> string: "orange and ba.ana and kiwi"
> expression: "\\.([a-z]+).*[ \t\n].*\\.([a-z]+)"

Looks like you mean "." at the beginning, not "\\." judging by the code below.

> expected result: match, captures 'range' and 'ana'
> real result: no match
>
> The code in question is this: [ ... ]
> result = regex_match(str, what, e);

According to the documentation, regex_match only finds matches that
consume ALL of the input text. You want regex_search. A test program
is attached. I double-backslashed the \t and \n, but verified that
either version works.

#include <string>
#include <iostream>
#include <iterator>

#include <boost/regex.hpp>

using namespace std;
using namespace boost;

int main ()
{
    string str = "orange and ba.ana and kiwi";

    try
        {
        smatch what;
        regex e (".([a-z]+).*[ \\t\\n].*\\.([a-z]+)");
        bool result = regex_search (str, what, e);

        if (result)
            cout << "Matched: " << what.format ("`$1' `$2'") << "\n";
        else
            cout << "No match for " << e << " in " << str << "\n";
        }
    catch (std::runtime_error& e)
        {
        cerr << e.what () << "\n";
        }

    return 0;
}

Output:
Matched: `range' `ana'

-- 
Caleb Epstein
caleb.epstein_at_[hidden]

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