Skip to content

Commit dd424c1

Browse files
committed
Merge #221 'Fix handling of optional regex captures'
2 parents ee740d2 + e7b8f64 commit dd424c1

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO
1111
* Statically linking `boost_system` ([#197](https://github.com/cucumber/cucumber-cpp/pull/197) Matthieu Longo)
1212
* Unable to `add_subdirectory(cucumber-cpp)` ([#211](https://github.com/cucumber/cucumber-cpp/pull/211) Sergey Bon)
1313
* Warning C4265 on Visual Studio ([#195](https://github.com/cucumber/cucumber-cpp/pull/195) Matthieu Longo)
14+
* Fix handling of optional regex captures ([#221](https://github.com/cucumber/cucumber-cpp/pull/221) Alain Martin)
1415

1516
## [0.5](https://github.com/cucumber/cucumber-cpp/compare/v0.4...v0.5) (2 July 2018)
1617

src/Regex.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ FindRegexMatch::FindRegexMatch(const boost::regex &regexImpl, const std::string
3737
if (i->matched) {
3838
RegexSubmatch s = {*i, i->first - expression.begin()};
3939
submatches.push_back(s);
40+
} else {
41+
submatches.push_back(RegexSubmatch());
4042
}
4143
}
4244
}

tests/unit/RegexTest.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ TEST(RegexTest, matchesRegexWithSubmatches) {
4646
EXPECT_EQ("69", match->getSubmatches()[1].value);
4747
}
4848

49+
TEST(RegexTest, matchesRegexWithOptionalSubmatches) {
50+
Regex sum("^(\\d+)\\+(\\d+)(?:\\+(\\d+))?=(\\d+)$");
51+
52+
shared_ptr<RegexMatch> match(sum.find("1+2+3=6"));
53+
EXPECT_TRUE(match->matches());
54+
ASSERT_EQ(4, match->getSubmatches().size());
55+
56+
match = shared_ptr<RegexMatch>(sum.find("42+27=69"));
57+
EXPECT_TRUE(match->matches());
58+
ASSERT_EQ(4, match->getSubmatches().size());
59+
EXPECT_EQ("42", match->getSubmatches()[0].value);
60+
EXPECT_EQ("27", match->getSubmatches()[1].value);
61+
EXPECT_EQ("", match->getSubmatches()[2].value);
62+
EXPECT_EQ("69", match->getSubmatches()[3].value);
63+
}
64+
4965
TEST(RegexTest, findAllDoesNotMatchIfNoTokens) {
5066
Regex sum("([^,]+)(?:,|$)");
5167
shared_ptr<RegexMatch> match(sum.findAll(""));

0 commit comments

Comments
 (0)