iak/regexbuilder

一个简化正则表达式编写的流畅API

v1.0 2017-10-24 17:13 UTC

This package is auto-updated.

Last update: 2024-09-09 06:06:22 UTC


README

一个简化正则表达式编写的流畅API。 (对于那些总是忘记语法的人来说)

安装

使用composer获取它

$ composer require iak/regexbuilder
{
    "require": {
        "iak/regexbuilder": "^1.0"
    }
}

就这么简单 :)

简介

这个库是给我们这些觉得正则表达式难以编写且难以记住所有不同标志、前瞻、捕获组等的人。

与其花半小时在Stack Overflow上搜索,不如希望你能用这个库轻松创建一个模式。

注意。仍然需要基本了解正则表达式。

快速开始

首先,在你的文件顶部使用这个类,

use RegexBuilder\Regex;

现在你可以这样使用它

$string = "wow! this is cool!"

$match = Regex::word("wow")->symbol("!")->match($string); // wow!

或者更高级一点(并展示一些不同的使用库的方式)

    // Match an email address

    $email = "info@isakberglind.se";

    Regex::group("a-z0-9_-.")
        ->oneOrMore()
        ->symbol("@")
        ->group("a-z0-9_-].")
        ->oneOrMore()
        ->symbol(".")
        ->group("a-z")
        ->count(2,6)
        ->match($email);

    // a simple url-matcher

    $url = "http://www.landslide-design.se";

    Regex::word(["http", "https", "ftp"])
        ->symbols("://")
        ->capture(function ($query) {
            return $query->symbols("www.");
        })
        ->optional()
        ->group("a-zA-Z0-9@:._")
        ->count(2, 255)
        ->symbols(".")
        ->group(function ($query) {
            return $query->range("a", "z");
        })
        ->count(2, 6)
        ->group(function ($query) {
            return $query
                ->range("a", "z")
                ->range("A", "Z")
                ->range(0, 9)
                ->symbols("@:_.?//");
        })
        ->zeroOrMore()
        ->match($url);

文档

单词、模式和符号


word(mixed $word = null)

匹配提供的单词、单词数组或任何单词

    $string = "This is a hard example!";

    Regex::word("simple")->replace("simple", $string);   // This is a simple example

    Regex::word(["This", "simple", "example"])->matchAll($string); // ["this", "example"]

    Regex::word()->matchAll($string) // ["this", "is", "a", "hard", "example"]

notWord()

匹配任何非单词

    $string = "Hi!!!!! What's up?";

    Regex::notWord()->match($string); // '!!!! '

symbols(string $symbols)

匹配提供的符号(转义字符串,如果你不想,请使用 "pattern")

    $string = "This is &!^@? awesome!"

    Regex::symbols("&!^@?")->replace("totally", $string) // This is totally awesome

pattern(string $pattern)

匹配提供的模式
别名: raw()

    $string = "kickass example text";

    Regex::pattern("(example|text)")->matchAll($string); // ["example", "text"]

字符


你可以使用以下辅助方法匹配一大堆字符

    Regex::digit();
    Regex::notDigit();
    Regex::whitespace();
    Regex::notWhitespace();
    Regex::char();
    Regex::notChar();
    Regex::hexDigit();
    Regex::octalDigit();
    Regex::newLine();
    Regex::carriageReturn();
    Regex::tab();
    Regex::verticalTab();
    Regex::formFeed();
    Regex::space();
    Regex::any();

量词


oneOrMore()

匹配前一个组、字符或字符集的一个或多个

    $string = "Here are some numbers 123456. Cool huh?"

    Regex::digit()->oneOrMore()->match($string) // 123456

zeroOrMore()

匹配零个或多个

    $string = "AA A1A A12A";

    Regex::char()->digit()->zeroOrMore()->char()->matchAll($string) // ["AA", "A1A", "A12A"]

count(int $count/$start, int $end = null)

匹配指定的数量或最小和最大计数

    $string = "1 12 123 1234";

    // Specify the exact count to match..
    Regex::digit()->count(3)->match($string); // 123

    // Or a minimum and maximum..
    Regex::digit()->count(2,4)->matchAll($string); // [12, 123, 1234]

组和字符集


range(mixed $start, $mixed $end)

指定一个范围,特别适用于与字符集一起使用

    Regex::range("a", "z"); // a-z

group(mixed $pattern/$callback)

创建一个字符集

    // Using a callback

    Regex::group(function ($builder) {
        return $builder->range("a", "z");
    });

    // Using a raw pattern

    Regex::group("a-z");

    // Produces the same;  [a-z]

捕获组


capture(callable $callback = null)

创建一个捕获组

    $string = "Capture this if you can!";

    // you can either capture the previous statement..

    Regex::word("this")->capture();

    // .. or using a callback

    Regex::capture(function ($builder) {
        return $builder->word("this");
    });

    // Produces the same; (this)

opionalCapture(mixed $pattern/$callback)

创建一个非捕获组

    $string = "Do not capture this if you can!";

    // you can either capture the previous statement..

    Regex::word("this")->capture();

    // .. or using a callback

    Regex::capture(function ($builder) {
        return $builder->word("this");
    });

    // Produces the same; (?:this)?

startCapture() endCapture()

你也可以用这些方法包围你想要捕获的内容

    $string = "Capture this if you can";

    Regex::startCapture()->word("this")->endCapture(); // (this)

前瞻和后顾


behind(mixed $pattern/$callback)

创建一个后顾
别名: beginsWith(), before()

    $string = "important";

    // Using a callback..
    Regex::behind(function ($builder) {
        return $builder->symbols("");
    })
    ->word()
    ->match($string);

    // .. or a raw pattern..
    Regex::behind("\*\*\*\*")->word()->match($string);

    // important

after(mixed $pattern/$callback)

创建一个前瞻,工作方式与before()相同
别名: endsWith()


其他辅助工具


optional(mixed $characters/$start = null, $length = null)

使捕获组、字符集或字符可选

    $string = "Is it spelled color or colour?";

    // Using a characters
    Regex::word("colour")->optional("u")->matchAll($string); // ["color", "colour"]

    // Using a start and a length
    Regex::word("colour")->optional(4,1)->matchAll($string); // ["color", "colour"]

    // Make last statement optinal

    Regex::symbols("colo")->char("u")->optional()->symbols("r")->matchAll($string); // ["color", "colour"]

escape(string $pattern)

转义提供的模式

    $pattern = "^[]$<";

    Regex::escape($pattern); // \^\[\]\$\<

getPattern()

返回构建的模式

    Regex::group("a-zA-Z")->oneOrMore()->symbols("!!")->optional()->zeroOrMore()->getPattern(); // /[a-zA-Z]+!!?*/

release()

移除构建的模式

    Regex::group("a-z")->symbol("!")->release()->symbols("only this")->getPattern(); // /only this/

匹配和替换


replace($string, $subject)

用提供的字符串替换构建的模式

    $string = "This is a hashtag: @. I'm sure!";

    Regex::symbol("@")->replace("#", $string); // This is a hashtag: #. I'm sure!

match($string)

匹配构建模式的第一次出现
注意!仅返回匹配项。如果你想要所有捕获组,请使用matchWithGroups()。

    $string = "Follow me on twitter: @Isak_Berglind!";

    Regex::symbol("@")->group("a-zA-Z_")->oneOrMore()->match($string); // @Isak_Berglind

matchAll($string)

匹配构建模式的全部出现
注意!仅返回匹配项。如果你想要所有捕获组,请使用matchAllWithGroups()。

    $string = "this is as good as it gets";

    Regex::any()->symbol("s")->matchAll($string); // ["is", "is", "as", "as", "ts"]