tags:

views:

65

answers:

1

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!

+4  A: 

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.

Tim Pietzcker
Thanks! This was actually a problem that popped up when porting code. The trailing slash had me totally stumped.
shuttle87