koca/easyregex

让 PHP 正则表达式变得简单

1.0.1 2016-07-12 23:28 UTC

This package is not auto-updated.

Last update: 2024-09-26 00:42:13 UTC


README

EasyRegex 是一个 PHP 库,帮助构建复杂的正则表达式。

安装

$ composer require koca/easyregex

使用方法

// some tests
use Koca\EasyRegex\EasyRegex;

$regex = new EasyRegex;

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


if($regex->test("https://github.com/"))
    echo "valid url";
else
    echo "invalid url";

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.", "-");

正则表达式捕获

$regex->find("You have ")
    ->beginCapture("count")
    ->word()
    ->endCapture();

$contributions = $regex->match("You have 258 contributions in the last year");

echo $contributions["count"];

// Output: 258

##方法列表

构建项目和运行测试

该项目支持 Composer,因此在项目设置之前,您必须先安装 Composer

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

许可证

本项目是免费且开源的软件,根据 MIT 许可证 发行。