Boost logo

Boost Users :

Subject: Re: [Boost-users] [Regex] Problem with optionality operator
From: Roland Bock (rbock_at_[hidden])
Date: 2009-09-14 08:37:03


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 <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")

The space is not optional in your expression.

Regards,

Roland


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