Skip to content

Commit 17b37af

Browse files
author
Hana Dusíková
committed
updated readme
1 parent dc91158 commit 17b37af

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ std::optional<date> extract_date(std::string_view s) noexcept {
124124
```
125125
[link to compiler explorer](https://gcc.godbolt.org/z/x64CVp)
126126
127+
##### Using captures
128+
```c++
129+
auto result = ctre::match<"(?<year>\\d{4})/(?<month>\\d{1,2})/(?<day>\\d{1,2})">(s);
130+
return date{result.get<"year">(), result.get<"month">, result.get<"day">};
131+
132+
// or in C++ emulation, but the object must have a linkage
133+
static constexpr ctll::fixed_string year = "year";
134+
static constexpr ctll::fixed_string month = "month";
135+
static constexpr ctll::fixed_string day = "day";
136+
return date{result.get<year>(), result.get<month>, result.get<day>};
137+
138+
// or use numbered access
139+
// capture 0 is the whole match
140+
return date{result.get<1>(), result.get<2>, result.get<3>};
141+
```
142+
127143
#### Lexer
128144
```c++
129145
enum class type {

tests/range.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ int main() {
2525

2626
#if __cpp_nontype_template_parameter_class
2727
std::cout << std::string_view(match.get<"first">()) << "\n";
28+
std::cout << std::string_view(match.get<1>()) << "\n";
2829
#else
2930
std::cout << std::string_view(match.get<name>()) << "\n";
31+
std::cout << std::string_view(match.get<1>()) << "\n";
3032
#endif
3133
}
3234
}

0 commit comments

Comments
 (0)