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

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


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

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


use Hexpress\Hexpress;

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

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.


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


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.