Boost logo

Boost Users :

Subject: [Boost-users] [Regex] Problem with optionality operator
From: Kay-Michael Wuerzner (wuerzner_at_[hidden])
Date: 2009-09-14 05:27:09


Hello,

I have got a problem with the interpretation of the optionality
operator '?' in boost::regex. Consider the following example
(regex_test.cpp):

#include <iostream>
#include <boost/regex.hpp>

int main ( int argc, char** argv )
{
    boost::regex test ( "(Apple)? Iphone 3g" );
    boost::match_results<std::string::const_iterator> what;

    std::string input ( argv[1] );

    if ( 0 == boost::regex_match ( input, what, test,
boost::match_default | boost::match_partial ) )
    {
        std::cerr << "No Match\n";
    }
    if ( what[0].matched )
    {
        std::cerr << "Full Match\n";
    }
    else
    {
        std::cerr << "Partial Match\n";
    }
    return 0;
}

Test runs are:
./regex_test "Apple Iphone"
Output: Partial Match (as expected)
./regex_test "Apple Iphone 3g"
Output: Full Match (as expected)
./regex_test "Iphone"
Output: No Match and Partial Match (Why is that? "Apple" is optional
should only be "Partial Match")
./regex_test "Iphone 3g"
Output: No Match and Partial Match (Why is that? "Apple" is optional
should be "Full Match")

Probably, I am only missing a flag but tests with "\A" and friends do
not help. Any comments are appreciated. Many thanks in advance.

Cheers,
Kay


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