[Regex] Problem with optionality operator

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

Kay-Michael Wuerzner wrote:
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
int main ( int argc, char** argv ) { boost::regex test ( "(Apple)? Iphone 3g" ); boost::match_resultsstd::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")
The space is not optional in your expression. Regards, Roland

./regex_test "Iphone" Output: No Match and Partial Match (Why is that? "Apple" is optional should only be "Partial Match")
No leading space in front of the "Iphone" compared to the regex which requires it as written.
./regex_test "Iphone 3g" Output: No Match and Partial Match (Why is that? "Apple" is optional should be "Full Match")
As above, your regex has a literal space in it which is not in the text. John.
participants (3)
-
John Maddock
-
Kay-Michael Wuerzner
-
Roland Bock