Reputation: 15924
I'm trying to write some c++ code that tests if a string is in a particular format. In this program there is a height followed by some decimal numbers: for example "height 123.45" or "height 12" would return true but "SomeOtherString 123.45" would return false.
My first attempt at this was to write the following:
string action;
cin >> action;
boost::regex EXPR( "^height \\d*(\\.\\d{1,2})?$/" ) ;//height format regex
bool height_format_matches = boost::regex_match( action, EXPR ) ;
if(height_format_matches==true){
\\do some stuff
}
However height_format_matches never seemed to be true. Any help is greatly appreciated!
Upvotes: 1
Views: 236
Reputation: 336108
Drop the trailing slash and it should work. Probably left over from a JavaScript regex? In JavaScript, regexes are often delimited by slashes; in C++, they are simply strings. If you keep the slash where it is, the regex engine is instructed to match a slash after the end of the string ($
), which always fails, of course.
Upvotes: 4