Home

Awesome

Build Status

PHPVerbalExpressions

VerbalExpressions is a PHP library that helps to construct hard regular expressions.

Installation

The project supports Composer so you have to install Composer first, before project setup.

$ composer require  verbalexpressions/php-verbal-expressions:dev-master

Examples

<?php
// some tests
require './vendor/autoload.php';
use VerbalExpressions\PHPVerbalExpressions\VerbalExpressions;

$regex = new VerbalExpressions();

$regex->startOfLine()
      ->then("http")
      ->maybe("s")
      ->then("://")
      ->maybe("www.")
      ->anythingBut(" ")
      ->endOfLine();


if ($regex->test("http://github.com")) {
    echo "valid url". '<br>';
} else {
    echo "invalid url". '<br>';
}

if (preg_match($regex, 'http://github.com')) {
    echo 'valid url';
} else {
    echo 'invalid url';
}


echo "<pre>". $regex->getRegex() ."</pre>";

echo $regex->clean(array("modifiers" => "m", "replaceLimit" => 4))
           ->find(' ')
           ->replace("This is a small test http://somesite.com and some more text.", "-");

More examples are available in the following files:

Business readable language expression definition

$definition = 'start, then "http", maybe "s", then "://", maybe "www.", anything but " ", end';
$regex = new VerbalExpressionsScenario($definition);

Methods list

NameDescriptionUsage
addadd values to the expressionadd('abc')
startOfLinemark expression with ^startOfLine(false)
endOfLinemark the expression with $endOfLine()
thenadd a string to the expressionadd('foo')
findalias for thenfind('foo')
maybedefine a string that might appear once or notmaybe('.com')
anythingaccept any stringanything()
anythingButaccept any string but the specified charanythingBut(',')
somethingaccept any non-empty stringsomething()
somethingButanything non-empty except for these charssomethingBut('a')
replaceshorthand for preg_replace()replace($source, $val)
lineBreakmatch \r \nlineBreak()
brshorthand for lineBreakbr()
tabmatch tabs \ttab()
wordmatch \w+word()
anyOfany of the listed charsanyOf('abc')
anyshorthand for anyOfany('abc')
rangeadds a range to the expressionrange(a,z,0,9)
withAnyCasematch case default case sensitivewithAnyCase()
stopAtFirsttoggles the g modifiersstopAtFirst()
addModifieradd a modifieraddModifier('g')
removeModifierremove a mofierremoveModifier('g')
searchOneLineToggles m modifiersearchOneLine()
multipleadds the multiple modifiermultiple('*')
_orwraps the expression in an or with the provided value_or('bar')
limitadds char limitlimit(1,3)
testperforms a preg_matchtest('valid@email.com')

For all the above method (except test) you could use the VerbalExpressionsScenario.

Other Implementations

You can see an up to date list of all ports on VerbalExpressions.github.io.

Building the project and running the tests

The project supports Composer so you have to install Composer first before project setup.

curl -sS https://getcomposer.org/installer | php
php composer.phar install --dev
ln -s vendor/phpunit/phpunit/phpunit.php phpunit
./phpunit