regexpbuilderphp

human-readable regular expressions for PHP7+

Github星跟踪图

 ____            _____            ____        _ _     _           ____  _   _ ____, _ \ ___  __ _, ____, _  ___ __, __ ) _   _(_), __, ___ _ __, _ \, _ \, _) / _ \/ _`, _, \ \/ / '_ \, _ \, / _`, / _ \ '__, _), _, _), _ <  __/ (_, ___ >  <, _), _), _, (_, __/, __/, _, __/, _, \_\___, \__,, _____/_/\_\ .__/, ____/ \__,_, _, _, \__,_, \___, _, _, _, _, _, ___/, _, ```
## human-readable regular expressions for PHP
[![Travis](https://img.shields.io/travis/gherkins/regexpbuilderphp.svg?style=flat-square)](https://travis-ci.org/gherkins/regexpbuilderphp)
[![Coveralls](https://img.shields.io/coveralls/gherkins/regexpbuilderphp.svg?style=flat-square)](https://coveralls.io/r/gherkins/regexpbuilderphp?branch=master)
[![release](https://img.shields.io/github/release/gherkins/regexpbuilderphp.svg?style=flat-square)](https://github.com/gherkins/regexpbuilderphp/releases)

PHP port of `regexpbuilderjs`

> RegExpBuilder integrates regular expressions into the programming language, thereby making them easy to read and maintain. Regular Expressions are created by using chained methods and variables such as arrays or strings.

## Installation

```bash
composer req gherkins/regexpbuilderphp

Or download the appropriate release and require RegExpBuilder.php and RegExp.php from /src.

Documentation

https://github.com/gherkins/regexpbuilderphp/wiki

Examples

use Gherkins\RegExpBuilderPHP;
$builder = new RegExpBuilder();

Validation

$regExp = $builder
    ->startOfInput()
    ->exactly(4)->digits()
    ->then("_")
    ->exactly(2)->digits()
    ->then("_")
    ->min(3)->max(10)->letters()
    ->then(".")
    ->anyOf(array("png", "jpg", "gif"))
    ->endOfInput()
    ->getRegExp();

//true
$regExp->matches("2020_10_hund.jpg");
$regExp->matches("2030_11_katze.png");
$regExp->matches("4000_99_maus.gif");

//false
$regExp->matches("123_00_nein.gif");
$regExp->matches("4000_0_nein.pdf");
$regExp->matches("201505_nein.jpg");
$regExp = $builder
    ->multiLine()
    ->globalMatch()
    ->min(1)->max(10)->anythingBut(" ")
    ->anyOf(array(".pdf", ".doc"))
    ->getRegExp();

$text = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy SomeFile.pdf eirmod tempor invidunt ut labore et dolore
magna aliquyam erat, sed diam voluptua. At vero eos et accusam
et justo duo dolores et ea rebum. doc_04.pdf Stet clita kasd File.doc.'

$matches = $regExp->findIn($text);

//true
($matches[0] === "SomeFile.pdf");
($matches[1] === "doc_04.pdf");
($matches[2] === "File.doc");

Replace

$regExp = $builder
    ->min(1)
    ->max(10)
    ->digits()
    ->getRegExp();

$text = "98 bottles of beer on the wall";

$text = $regExp->replace(
    $text,
    function ($match) {
        return (int)$match + 1;
    }
);

//true
("99 bottles of beer on the wall" === $text);

Validation with multiple patterns

$a = $builder
    ->startOfInput()
    ->exactly(3)->digits()
    ->anyOf(array(".pdf", ".doc"))
    ->endOfInput();

$b = $builder
    ->getNew()
    ->startOfInput()
    ->exactly(4)->letters()
    ->then(".jpg")
    ->endOfInput();

$regExp = $builder
    ->getNew()
    ->eitherFind($a)
    ->orFind($b)
    ->getRegExp();

//true
$regExp->matches("123.pdf");
$regExp->matches("456.doc");
$regExp->matches("bbbb.jpg");
$regExp->matches("aaaa.jpg");

//false
$regExp->matches("1234.pdf");
$regExp->matches("123.gif");
$regExp->matches("aaaaa.jpg");
$regExp->matches("456.docx");

Take a look at the tests for more examples

主要指标

概览
名称与所有者gherkins/regexpbuilderphp
主编程语言PHP
编程语言PHP (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2015-02-12 19:16:22
推送于2024-08-26 05:21:32
最后一次提交
发布数13
最新版本名称2.1.1 (发布于 )
第一版名称0.7.0 (发布于 )
用户参与
星数1.3k
关注者数49
派生数49
提交数206
已启用问题?
问题数17
打开的问题数0
拉请求数10
打开的拉请求数0
关闭的拉请求数3
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?