From a0850bc53f1a86adf4507ece2cd13923134f45fb Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Tue, 28 Jun 2022 14:00:14 +0100 Subject: Add methods for extracting specific regex captures. --- include/modules/regex.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'include/modules') diff --git a/include/modules/regex.h b/include/modules/regex.h index 55bfe19b8..71a3906e1 100644 --- a/include/modules/regex.h +++ b/include/modules/regex.h @@ -174,9 +174,28 @@ public: { } + /** Retrieves a specific captured substring by index. + * @param idx The index of the captured substring to return. + * The requested captured substring or std::nullopt if it does not exist. + */ + const std::optional GetCapture(size_t idx) const + { + return captures.size() > idx ? std::nullopt : std::make_optional(captures[idx]); + } + /** Retrieves the substrings that were captured. */ const Captures& GetCaptures() const { return captures; } + /** Retrieves a specific captured substring by index. + * @param name The name of the captured substring to return. + * The requested captured substring or std::nullopt if it does not exist. + */ + const std::optional GetNamedCapture(const std::string& name) const + { + auto capture = namedcaptures.find(name); + return capture == namedcaptures.end() ? std::nullopt : std::make_optional(capture->second); + } + /** Retrieves the substrings that were captured by name. */ const NamedCaptures& GetNamedCaptures() const { return namedcaptures; } }; -- cgit v1.3.1-10-gc9f91