承接 iak/regexbuilder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

iak/regexbuilder

Composer 安装命令:

composer require iak/regexbuilder

包简介

A fluent api that simplifies writing regular expressions

README 文档

README

A fluent api that simplifies writing regular expressions. (for the ones of us who always forget the syntax)

Installation

Grab it using composer

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

Simple as that :)

Introduction

This library is for all of us that find regular expressions hard to write and impossible to remember all the different flags, look aheads, capture groups etc.

Instead of spending that half hour searching stackoverflow, I hope you can easily whip up a pattern using this lib.

Note. a basic understading of how regular expressions is still needed.

Quick start

First of all, use the class at the top of you file,

use RegexBuilder\Regex;

Now you can use it like this

$string = "wow! this is cool!"

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

Or maybe something more advanced (and demostrating some different ways of using the library)

    // 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);

Documentation

Words, patterns and symbols


word(mixed $word = null)

Matches provided word, array of words or any word

    $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()

Matches anything but a word

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

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

symbols(string $symbols)

Matches provided symbols (escapes string, if you don't want that, use "pattern")

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

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

pattern(string $pattern)

Matches provided pattern
Aliases: raw()

    $string = "kickass example text";

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

Characters


You can match a bunch of characters using the following helper methods

    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();

Quantifiers


oneOrMore()

Matches one or more of preceding group, character or character set.

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

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

zeroOrMore()

Matches zero or more

    $string = "AA A1A A12A";

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

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

Matches the specified amount or the minimum and maximun count

    $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]

Groups & Character sets


range(mixed $start, $mixed $end)

Specifies a range, made especially for working with character sets

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

group(mixed $pattern/$callback)

Creates a character set

    // 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 groups


capture(callable $callback = null)

Creates a capture group

    $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)

Creates a non capturing group

    $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() and endCapture()

You can also surround what you want to capture with these methods

    $string = "Capture this if you can";

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

Look aheads & look behinds


behind(mixed $pattern/$callback)

Creates a look behind
Aliases: 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)

Creates a look ahead, works exactly like before()
Aliases: endsWith()


Other helpers


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

Makes capture group, character set or character optional

    $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)

Escapes provided pattern

    $pattern = "^[]$<";

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

getPattern()

Returs the built up pattern

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

release()

Removes built up pattern

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

Matching and replacing


replace($string, $subject)

Replace built up pattern with provided string

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

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

match($string)

Matches the first occurrence of the built up pattern
Note! only return the match. If you want all capture groups, use matchWithGroups()

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

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

matchAll($string)

Matches all of the occurences of the built up pattern
Note! only return the match. If you want all capture groups, use matchAllWithGroups()

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

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

iak/regexbuilder 适用场景与选型建议

iak/regexbuilder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 5, 最近一次更新时间为 2017 年 10 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「regex」 「regular expressions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 iak/regexbuilder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 iak/regexbuilder 我们能提供哪些服务?
定制开发 / 二次开发

基于 iak/regexbuilder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 22
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-24