2

Trying to figure out how to extract groups of 4 digit using regex

The regex I'm using now :

regex_time : "(([01][0-9]|2[0-3])[0-5][0-9])";

Code sample:

regex expressionFormat(REGEX_TIME);
boost::match_results<std::string::const_iterator> what;

if (boost::regex_search(input,what,expressionFormat))

My input would usually be in the form of

"0000 1800 2359"

And I would like to push them into a vector to do some comparsion.

It's for parsing time in string format from a line.

Larry Lee
  • 61
  • 1
  • 6

1 Answers1

0

You need to use a while loop instead of if for regex_search:

string input;
boost::regex regex("(([01][0-9]|2[0-3])[0-5][0-9])");
boost smatch;

while (boost::regex_search(input.start(), input.end(), what, regex)) {
    // what[0] contains current full match
    // what[2] contains the hours
}

Note that each loop will move what to the next value. See the example on the boost doc page: http://www.boost.org/doc/libs/1_42_0/libs/regex/doc/html/boost_regex/ref/regex_search.html

The values of indices in what are determined by your capture groups in the expression. If you wanted to create another group to match minutes, simply create a new group:

"(([01][0-9]|2[0-3])([0-5][0-9]))"

To determine the group index in what, count forward the number of open parenthesis.

jheddings
  • 26,717
  • 8
  • 52
  • 65