add Regex lib

This commit is contained in:
Pekka Laiho 2020-10-18 14:33:27 +07:00
parent c0fb53742a
commit 06acb47c72
3 changed files with 57 additions and 0 deletions

View File

@ -270,6 +270,16 @@ sqrt | `(sqrt 2)` | `1.41` | Calculate the square root.
coinflip | `(coinflip)` | `true` | Return true or false with equal probability.
rand | `(rand 5 10)` | `8` | Return a random integer between given min and max values.
### Regular expression functions
Name | Example | Example result | Description
------------- | ------- | -------------- | -----------
re-match | `(re-match "/^[a-z]{4}[0-9]{4}$/" "test1234")` | `true` | Match subject to regular expression using [preg_match](https://www.php.net/manual/en/function.preg-match.php).
re-match | `(re-match "/[a-z]{5}/" "one three five" true)` | `"three"` | Give true as third argument to return the matched text.
re-match-all | `(re-match-all "/[A-Z][a-z]{2}[0-9]/" "One1 Two2 Three3")` | `["One1" "Two2"]` | Find multiple matches to regular expression using [preg_match_all](https://www.php.net/manual/en/function.preg-match-all.php).
re-replace | `(re-replace "/year ([0-9]{4}) month ([0-9]{2})/" "$1-$2-01" "year 2020 month 10")` | `"2020-10-01"` | Perform search and replace with regular expression using [preg_replace](https://www.php.net/manual/en/function.preg-replace.php).
re-split | `(re-split "/\\s+/" "aa bb cc ")` | `["aa" "bb" "cc"]` | Split the subject by regular expression using [preg_split](https://www.php.net/manual/en/function.preg-split.php). The flag `PREG_SPLIT_NO_EMPTY` is enabled.
### String functions
Name | Example | Example result | Description

46
src/Lib/Regex.php Normal file
View File

@ -0,0 +1,46 @@
<?php
namespace MadLisp\Lib;
use MadLisp\CoreFunc;
use MadLisp\Env;
use MadLisp\Vector;
class Regex implements ILib
{
public function register(Env $env): void
{
// More options need to be added for advanced usage. Basic functionality provided for now.
$env->set('re-match', new CoreFunc('re-match', 'Return true if second argument matches with the regular expression given as first argument. Return the matched text if third argument is true.', 2, 3,
function (string $pattern, string $subject, bool $returnMatch = false) {
if ($returnMatch) {
if (preg_match($pattern, $subject, $matches)) {
return $matches[0];
} else {
return null;
}
} else {
return boolval(preg_match($pattern, $subject));
}
}
));
$env->set('re-match-all', new CoreFunc('re-match-all', 'Return all matches in second argument to regular expression given as first argument.', 2, 2,
function (string $pattern, string $subject) {
if (preg_match_all($pattern, $subject, $matches) > 0) {
return new Vector($matches[0]);
} else {
return new Vector();
}
}
));
$env->set('re-replace', new CoreFunc('re-replace', 'Replace matches to regular expression (first argument) by the second argument in the subject (third argument).', 3, 4,
fn (string $pattern, string $replacement, string $subject, int $limit = -1) => preg_replace($pattern, $replacement, $subject, $limit)
));
$env->set('re-split', new CoreFunc('re-split', 'Split second argument by regular expression given as first argument.', 2, 3,
fn (string $pattern, string $subject, int $limit = -1) => new Vector(preg_split($pattern, $subject, $limit, PREG_SPLIT_NO_EMPTY))
));
}
}

View File

@ -64,6 +64,7 @@ class LispFactory
(new Lib\IO())->register($env);
(new Lib\Json())->register($env);
(new Lib\Math())->register($env);
(new Lib\Regex())->register($env);
(new Lib\Strings())->register($env);
(new Lib\Time())->register($env);
(new Lib\Types())->register($env);