Project

General

Profile

Regex » History » Version 3

Yingdi Yu, 03/19/2014 11:19 AM

1 1 Yingdi Yu
# NDN Regular Expression
2 2 Yingdi Yu
3
NDN regular expression matching is done at two levels: one at the name level and one at the name component level.
4
5
We use `<` and `>` to enclose a name component matcher which specifies the pattern of a name component.
6
The component pattern is expressed using the [Perl Regular Expression Syntax](http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html).
7
For example, `<ab*c>` can match the 1st, 3rd, and 4th components of `"/ac/dc/abc/abbc"`, but it cannot match the 2nd component.
8
A special case is that `<>` is a wildcard matcher that can match **ANY** component.
9
10
Note that a component match can match only one name component.
11
In order to match a name, you need to specify the pattern of a name based on the name component matchers.
12
For example, `<ndn><edu><ucla>` can match the name `"/ndn/edu/ucla"`.
13
In order to describe a more complicated name pattern, we borrow some syntaxes from the standard regular expressions.
14
15
## NDN Regex Syntax
16
17
### Anchors
18
19 3 Yingdi Yu
A `'^'` character shall match the start of a name.
20 2 Yingdi Yu
For example, `^<ndn>` shall match any names starting with a component `"ndn"`, and it will exclude a name like `"/local/broadcast"`.
21
22 3 Yingdi Yu
A `'$'` character shall match the end of a name.
23 2 Yingdi Yu
For example, `^<ndn><edu>$` shall match only one name: `"/ndn/edu"`. 
24
25
26
A NDN regular expression is built on the component matcher extended from the standard regex by simply treating a component matcher as a single character in standard regex.
27
For example, 
28
`^<ndn>` can match any names starting with a component `"ndn"`; 
29
`<test>$` can match any names ending with a component `"test"`;
30
`^[^<ndn>]` can match any names that do not start with a component `"ndn"`;
31
`^([^<DNS>])<DNS>(<>*)<NS>` can match a NDN DNS data name, and you can use back reference to extract the part enclosed by `(` and `)`
32
(`"\1\2"` can extract `/ndn/edu/ucla/irl` from `/ndn/edu/ucla/DNS/irl/NS/123456`.