3

Suppose I have this simple regular expression search piece of code:

boost::regex re("(\\d+)(/(\\d))?");
boost::smatch matches;
boost::regex_search(input_str, matches, re);

It searches a string for something like 123/2 or 123. Second digit and / are optional.
I want to know that if /2 is present or not and if it exists set the second number after / to a variable or set the variable to -1 otherwise.
I tried to use matches.size(), but it's always the same value whether the second part exists or not.

s4eed
  • 7,173
  • 9
  • 67
  • 104

1 Answers1

2

The groups have a boolean matched member that you can check.

See http://www.boost.org/doc/libs/1_59_0/libs/regex/doc/html/boost_regex/ref/regex_match.html

m[b].matched - For all integers n < m.size(), true if sub-expression n participated in the match, false otherwise.

sehe
  • 374,641
  • 47
  • 450
  • 633