Matchers¶
Cripts supports most common comparisons, ==
, !=
, etc. for many of the
strings and variables that it provides. However, this is sometimes not adequate
for all use cases, so Cripts provides a way to define custom matchers. There are
currently three types of matchers:
Matcher |
Description |
---|---|
|
Matching IP addresses against one or many IP ranges. |
|
Matching strings against one or many regular expressions. |
|
Match a request method against a list of methods. |
Often you will declare these ranges once, and then use them over and over again. For this purpose,
Cripts allows ranges to be declared static
, which means it can optimize the code around the matches.
Here's an example using the regular expression matcher:
do_remap()
{
static cripts::Matcher::PCRE pcre({"^/([^/]+)/(.*)$", "^(.*)$"}); // Nonsensical ...
borrow url = cripts::Client::URL::Get();
if (pcre.Match(url.path)) {
// Do something with the match
}
}
注釈
For the IP-range and regular expression matcher, you can specify a single range or regular expression,
it does not have to be declared as a list with the {}
syntax. For both, the single or list arguments
are strings within ""
.
Matcher Functions¶
All matchers have the following functions:
Function |
Description |
---|---|
|
Match the given string against the matcher. |
|
Another name for |
|
Add another element to the matcher (can not be used with |
Matcher::PCRE¶
The PCRE matcher is used to match strings against one or many regular expressions. When a match
is found, a cripts::Matcher::PCRE::Result
object is returned. This object has the following functions
to deal with the matched results and the capture groups:
Function |
Description |
---|---|
|
A boolean indicating if a regex was matched. |
|
Returns the number of regex capture groups that are matched. |
|
Returns the index of the matched regex capture group. |
[] Index |
Retrieves the matched string for the given capture group index. |
Lets show an example:
do_remap()
{
static cripts::Matcher::PCRE allow({"^([a-c][^/]*)/?(.*)", "^([g-h][^/]*)/?(.*)"});
borrow url = cripts::Client::URL::Get();
auto res = allow.Match(url.path);
if (res.Matched()) {
CDebug("Matched: {}", res[1]);
CDebug("Matched: {}", res[2]);
// Now do something with these URLs matching these paths
}
}