diff --git a/README.md b/README.md index 756c5db..1d83194 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Lib/Regex.php b/src/Lib/Regex.php new file mode 100644 index 0000000..c3ec237 --- /dev/null +++ b/src/Lib/Regex.php @@ -0,0 +1,46 @@ +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)) + )); + } +} diff --git a/src/LispFactory.php b/src/LispFactory.php index cb870bf..5c9a684 100644 --- a/src/LispFactory.php +++ b/src/LispFactory.php @@ -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);