xp-lang / php-pattern-operator
PHP的模式运算符
v0.2.0
2024-03-24 13:08 UTC
Requires
- php: >=7.4.0
- xp-framework/compiler: ^9.0 | ^8.0 | ^7.0
- xp-framework/core: ^12.0 | ^11.0 | ^10.0
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
README
这是一个为XP 编译器提供的插件,该插件为PHP语言添加了一个中缀运算符~
。
示例
模式运算符将以下表达式视为正则表达式,并返回其生成的匹配项
if ($greeting ~ '/^H[ea]llo/') { // Matches! } if ($matches= $greeting ~ '/^H[ea]llo/') { // Work with $matches }
模式将被传递到preg_match,因此上述代码等同于以下代码
if (preg_match($greeting, '/^H[ea]llo/')) { // Matches! } if (preg_match($greeting, '/^H[ea]llo/', $matches)) { // Work with $matches }
安装
将XP编译器安装到您的项目中后,还需要包含此插件。
$ composer require xp-framework/compiler # ... $ composer require xp-lang/php-pattern-operator # ...
无需进行其他操作。
另请参阅
- xp-framework/compiler#130 动机
- https://www.perltutorial.org/perl-regular-expression/
- https://stackoverflow.com/questions/19441521/bash-regex-operator
- https://puppet.com/docs/puppet/latest/lang_expressions.html (
=~
和!~
) - https://github.com/kasei/SwiftRegex 和 https://github.com/johnno1962/SwiftRegex
- https://externals.io/message/4825 (PHP内部讨论,2003)