Git Product home page Git Product logo

phpverbalexpressions's Introduction

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

Name Description Usage
add add values to the expression add('abc')
startOfLine mark expression with ^ startOfLine(false)
endOfLine mark the expression with $ endOfLine()
then add a string to the expression add('foo')
find alias for then find('foo')
maybe define a string that might appear once or not maybe('.com')
anything accept any string anything()
anythingBut accept any string but the specified char anythingBut(',')
something accept any non-empty string something()
somethingBut anything non-empty except for these chars somethingBut('a')
replace shorthand for preg_replace() replace($source, $val)
lineBreak match \r \n lineBreak()
br shorthand for lineBreak br()
tab match tabs \t tab()
word match \w+ word()
anyOf any of the listed chars anyOf('abc')
any shorthand for anyOf any('abc')
range adds a range to the expression range(a,z,0,9)
withAnyCase match case default case sensitive withAnyCase()
stopAtFirst toggles the g modifiers stopAtFirst()
addModifier add a modifier addModifier('g')
removeModifier remove a mofier removeModifier('g')
searchOneLine Toggles m modifier searchOneLine()
multiple adds the multiple modifier multiple('*')
_or wraps the expression in an or with the provided value _or('bar')
limit adds char limit limit(1,3)
test performs a preg_match test('[email protected]')

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

phpverbalexpressions's People

Contributors

4d47 avatar akalicki avatar bryant1410 avatar bytehead avatar chriskonnertz avatar christiangaertner avatar felixdorn avatar frastel avatar go-oleg avatar hirakc avatar idrinth avatar jehna avatar jmingov avatar mihai-vlc avatar niklongstone avatar peter279k avatar readmecritic avatar ronfroy avatar signpostmarv avatar xzero707 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phpverbalexpressions's Issues

Providing a tagged version?

I'm wondering if the library is by now stable enough to be given a tag - likely 1.0.0 - so it's less risky to depend on this library.
Having a version just makes it way easier to tie down a specific api.

Support for PHP 5.3-5.5

I was wondering if support for 5.3 to 5.5 would be an option, if the slow func_get_args would be just a different implementation of the single method, provided in another class.

A (new) abstract class would use: protected function range(array $args)
The existing class would just call parent::range($args)
A new class for older php would use parent::range(func_get_args())

That would allow older versions to work without negatively impacting the performance of newer versions of php and the change would not break current code.
Tests would need to be expanded to check that both range-implementations work the same if covering the single new line is desired.

If desired I'll write the required code, I'd like to have a 5.3 compatible version in any case.

anythingBut and a range doesn't work

Hello,

It seems it's not possible to do something like:

$regex->anythingBut('0-9')

since the sanitize method add systematically a "\" behind the "-" which break the regex range expression.
That giving us:

(?:[^0\-9]*)
instead of
(?:[^0-9]*)

Thanks a lot,
Aurélien

Length restrictions?

Hey, does phpve support length restrictions like: \d{3}[A-Z]{2} ? I couldn't find it.

query regarding getRegex testing

I've noticed that JSVerbalExpressions & PHPVerbalExpressions don't produce the same output for the same input; Which of the VerbalExpressions projects is considered the "master" to test against?

example:
input:
regex->startOfLine()->range(0, 9, 'a', 'z', 'A', 'Z')->multiple('')

$js = '/^[0-9a-zA-Z](?:)*/gm';
$php = '/^[0-9a-zA-Z]+/m';

(variables edited to clarify language)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.