2017 © Pedro Peláez
 

library xpressions

A fluent php api for regular expressions.

image

alshahawi/xpressions

A fluent php api for regular expressions.

  • Thursday, March 2, 2017
  • by AlShahawi
  • Repository
  • 3 Watchers
  • 5 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Xpressions

A fluent PHP API for regular expressions., (*1)

Installation

Xpressions now is not ready for production yet, but you can install it using composer via:, (*2)

composer require alshahawi/xpressions:dev-master

Examples

Creating an instance

$matcher = Xpressions::match();

Matchting a string

$matcher = Xpressions::match()->exact('foo');

var_dump($matcher->test('bar')); // -> false
var_dump($matcher->test('foo')); // -> true

Inline Matching

In some situations (like validation) you might want to match something in the start of the line and before the end of that line, in that case you might use begin() and end() methods. For example:, (*3)

$matcher = Xpressions::match()->begin()->exact('bar')->end();

var_dump($matcher->test('foo bar')); // -> false
var_dump($matcher->test('bar foo')); // -> false
var_dump($matcher->test('bar'));     // -> true

NOTE: without begin() and end() all of the above tests will match (returning true), because whatever the matched in the start or the end of the line will return true unless you specified the begin and the end of the line (the whole text)., (*4)

Matching an optional string

$matcher = Xpressions::match()->exact('foo')->maybe('bar')->exact('baz');

var_dump($matcher->test('foo'));       // -> false
var_dump($matcher->test('foobar'));    // -> false
var_dump($matcher->test('foobarbaz')); // -> true
var_dump($matcher->test('foobaz'));    // -> true

Matching alphanumeric & underscores

$matcher = Xpressions::match()->word();

var_dump($matcher->test('!')); // -> false
var_dump($matcher->test('a')); // -> true
var_dump($matcher->test('1')); // -> true
var_dump($matcher->test('_')); // -> true

You may use words() to match a one or more word characters., (*5)

You may use nonWord() as an inverse to this method., (*6)

Matching a digit

$matcher = Xpressions::match()->digit();

var_dump($matcher->test('!')); // -> false
var_dump($matcher->test('a')); // -> false
var_dump($matcher->test('1')); // -> true

You may use digits() to match a one or more word characters., (*7)

You may use nonDigit() as an inverse to this method., (*8)

Matching any of a given values

$matcher = Xpressions::match()->any('foo', 'bar', 'baz');

var_dump($matcher->test('something')); // -> false
var_dump($matcher->test('foo'));       // -> true
var_dump($matcher->test('bar'));       // -> true
var_dump($matcher->test('baz'));       // -> true

Advanced Examples

Matching an email address

$matcher = Xpressions::match()
    ->begin() // match a line start
    ->oneOrMore(function($xpr) {
        $xpr->word()->or('.');
    })
    ->exact('@')
    ->words()
    ->oneOrMore(function($xpr) {
        $xpr->maybe('.')
            ->word();
    })
    ->end(); // match a line end

var_dump($matcher->test('invalid'));        // ->false
var_dump($matcher->test('me@example.com')); // ->true

Interested in more advanced examples?

Many of examples will be written and documented soon!, but you can view tests/XpressionsTest.php for now., (*9)

The Versions

02/03 2017

dev-master

9999999-dev https://github.com/AlShahawi/xpressions

A fluent php api for regular expressions.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Ahmed Al-Shahawi

regex regular-expressions