sizuhiko/hexpress

hexpress 是一个 PHP 库,它提供了一种人类可读的方式来定义正则表达式

v0.1.1 2016-05-29 07:56 UTC

This package is auto-updated.

Last update: 2024-09-26 16:35:21 UTC


README

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License

hexpress

hexpress 是一个 PHP 库,它提供了一种人类可读的方式来定义正则表达式

hexpress 是从 Ruby 的 hexpress 移植过来的。

hexpress 是对 "Verbal Hexpressions" 概念的另一种尝试。

hexpress 需要 PHP >= 5.5

使用

use Hexpress\Hexpress;

$pattern = (new Hexpress())
    ->start("http")
    ->maybe("s")
    ->with("://")
    ->maybe(function($hex) { $hex->words()->with("."); })
    ->find(function($hex) { $hex->matching(function($hex) {$hex->word()->with("-");})->many(); })
    ->has(".")
    ->either(["com", "org"])
    ->maybe("/")
    ->ending();

use Hexpress\Hexpress 之后,您将能够访问 Hexpress 类,这允许您链式调用方法来构建正则表达式模式。

您可以通过调用 Hexpress#__toString()Hexpress#toRegExp 来查看此模式。

echo $pattern;             #=> "^https?\:\/\/(?:(?:\w)+\.)?([\w\-]+)\.(?:com|org)\/?$"
echo $pattern->toRegExp(); #=> "/^https?\:\/\/(?:(?:\w)+\.)?([\w\-]+)\.(?:com|org)\/?$/"

您甚至可以进行多个模式的复杂组合

$protocol = (new Hexpress())->start("http")->maybe("s")->with("://");
$tld = (new Hexpress())->with(".")->either(["org", "com", "net"]);
$link = (new Hexpress())->has($protocol)->find(function($hex) {$hex->words();})->including($tld);

echo $link; #=> "^https?\:\/\/((?:\w)+)\.(?:org|com|net)"

Hexpressions 非常灵活。

TODO

  • PHP 文档和 API 参考
  • look-ahead assertions(前瞻断言)
  • look-behind assertions(后瞻断言)
  • back reference(回溯引用)
  • conditional subpattern(条件子模式)
  • once-only subpattern(一次性子模式)
  • 内部选项设置

安装

将此行添加到您应用程序的 composer.json 中

    "require": {
        "sizuhiko/hexpress": ">=1.0"
    },

然后,执行

$ composer install

或者您可以自己安装

$ composer require "sizuhiko/hexpress:>=1.0"

为此库贡献力量

请随意通过提出新问题、请求、单元测试和代码修复或新功能来为此库贡献力量。如果您想贡献一些代码,请从 develop 创建一个功能分支,并给我们发送您的 pull request。