Added the named capture explanation in README.

This commit is contained in:
yhirose 2015-06-13 17:11:27 -04:00
parent 6d8826741a
commit 652e25a10b

View File

@ -175,6 +175,7 @@ Simple interface
```c++
peglib::match m;
auto ret = peglib::peg_match(
R"(
ROOT <- _ ('[' $< TAG_NAME > ']' _)*
@ -191,6 +192,26 @@ assert(m.str(2) == "tag:2");
assert(m.str(3) == "tag-3");
```
It also supports named capture with the `$name<` ... `>` operator.
```c++
peglib::match m;
auto ret = peglib::peg_match(
" ROOT <- _ ('[' $test< TAG_NAME > ']' _)* "
" TAG_NAME <- (!']' .)+ "
" _ <- [ \t]* ",
" [tag1] [tag:2] [tag-3] ",
m);
auto cap = m.named_capture("test");
REQUIRE(ret == true);
REQUIRE(m.size() == 4);
REQUIRE(cap.size() == 3);
REQUIRE(m.str(cap[2]) == "tag-3");
```
There are some ways to *search* a peg pattern in a document.
```c++