hexpress

hexpress is a PHP library that human way to define regular expressions

  • Owner: sizuhiko/hexpress
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

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

hexpress

hexpress is a PHP library that human way to define regular expressions

hexpress is ported from Ruby's hexpress.

The hexpress is another take at the concept of "Verbal Hexpressions".

hexpress requires PHP >= 5.5

Using

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();

After use Hexpress\Hexpress you'll have access to the Hexpress class, which allows you to chain methods to build up a regex pattern.

You can see this pattern by calling either Hexpress#__toString() or Hexpress#toRegExp:

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

You can even do advanced composure of multiple patterns:

$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 are very flexible.

TODO

  • PHP Document and API reference
  • lookahead assertions
  • lookbehind assertions
  • back reference
  • conditional subpattern
  • once-only subpattern
  • Internal option setting

Installing

Add this line to your application's composer.json:

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

And then execute:

$ composer install

Or install it yourself as:

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

Contributing to this Library

Please feel free to contribute to the library with new issues, requests, unit tests and code fixes or new features.
If you want to contribute some code, create a feature branch from develop, and send us your pull request.

Main metrics

Overview
Name With Ownersizuhiko/hexpress
Primary LanguagePHP
Program languagePHP (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2015-09-13 08:11:22
Pushed At2016-05-29 08:42:39
Last Commit At2016-05-29 17:42:38
Release Count2
Last Release Namev0.1.1 (Posted on )
First Release Namev0.1.0 (Posted on )
用户参与
Stargazers Count41
Watchers Count4
Fork Count0
Commits Count52
Has Issues Enabled
Issues Count0
Issue Open Count0
Pull Requests Count4
Pull Requests Open Count0
Pull Requests Close Count0
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private