[Regex] Which sub-expression did participate in a partial match?
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
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?
You do get marked sub-expression positions in partial matches, *but only if the sub-expression was completely matched*. Obviously that is not the case here I'm afraid. John. ps, (*it).position() in your code should be (*it).position(i).
participants (2)
-
John Maddock
-
Kay-Michael Wuerzner