Boost logo

Boost Users :

Subject: [Boost-users] [Regex] Which sub-expression did participate in a partial match?
From: Kay-Michael Wuerzner (wuerzner_at_[hidden])
Date: 2009-09-18 05:25:54


Hello,

I wonder if it is possible to find out which sub-expression of a regex
is responsible for a partial match? Consider the following example:

int main ( int argc, char** argv )
{
    std::string input ( argv[1] );
    boost::regex test ( "((?:Apple )?iPhone)|(Sony Vaio)" );
    boost::sregex_iterator it ( input.begin(), input.end (), test,
boost::match_default | boost::match_partial );
    boost::sregex_iterator none;

    if ( it != none )
    {
        std::cout << (*it).size () << std::endl;
        if ( (*it)[0].matched )
            std::cout << "full match\n";
        else
            std::cout << "partial match\n";
        for ( unsigned int i = 1; i < (*it).size (); ++i )
        {
            if ( (*it)[i].matched ) // this should in principle also
be true for partial matches
                std::cout << i << " " << (*it).position () << std::endl;
         }
    }
    return 0;
}
The problem is that the if-condition in the for loop is not met in
case of a partial match on the sub-expression:
$> ./regex_test "Apple"
partial match
$> ./regex_test "iPhone"
full match
1 0
In the first case I would expect the output of the sub-expression and
the position of the match. Are you aware of any method to achieve this
behavior?

Many thanks in advance,
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