PHPVerbalExpressions

PHP Regular expressions made easy

Github星跟蹤圖

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)
endoOfLine, 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('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

主要指標

概覽
名稱與所有者VerbalExpressions/PHPVerbalExpressions
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2013-07-22 17:55:17
推送於2020-06-13 05:13:01
最后一次提交2020-06-13 07:13:00
發布數1
最新版本名稱1.0.0 (發布於 )
第一版名稱1.0.0 (發布於 )
用户参与
星數2.4k
關注者數123
派生數143
提交數119
已啟用問題?
問題數8
打開的問題數2
拉請求數27
打開的拉請求數0
關閉的拉請求數2
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?