hoels/regex 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

hoels/regex

Composer 安装命令:

composer require hoels/regex

包简介

A wrapper around regular expressions.

README 文档

README

This is a wrapper around regular expressions.

Regular expressions in PHP are a common point of failure and working with match groups in particular can be a major headache. Inspired by the functionality Kotlin offers with its Regex class, I decided to start implementing a similar functionality for PHP. Hoping that one day, the PHP team will improve the preg_* functions, this library provides a very simple wrapper around regular expressions in PHP.

In contrast to the Kotlin implementation, the delimiters (e.g. / at the beginning and end of the expression) have to be set. This allows for greater flexibility.

Installation

Requirements

  • PHP 8.1+

Composer

composer require hoels/regex

Documentation

Regex class

The main class of this library is the Regex class. The class currently provides the following functions:

containsMatchIn

Regex::containsMatchIn(string $regex, string $input): bool

Indicates whether there is at least one match in $input for the regex given in $regex.

find

Regex::find(string $regex, string $input): MatchResult|null

Returns the first match in $input for the regex given in $regex. Returns null if there is no match and an MatchResult object if there is a match.

findAll

Regex::findAll(string $regex, string $input): MatchResult[]

Returns an array of MatchResult objects for all matches in $input for the regex given in $regex. Returns an empty array if there is no match.

matchAt

Regex::matchAt(string $regex, string $input, int $index): MatchResult|null

Returns the first match in $input for the regex given in $regex, only if the match starts at $index. Returns null if there is no match and an MatchResult object if there is a match at $index.

replace

Regex::replace(string $regex, string $input, string $replacement): string

Replaces all occurrences of $regex in $input by the replacement expression $replacement. Example:

echo Regex::replace("/(\\d\\.\\d)\\.\\d+/", "We support PHP 8.1.26 and 8.2.13.", "$1"); // We support PHP 8.1 and 8.2.

replaceFirst

Regex::replaceFirst(string $regex, string $input, string $replacement): string

Replaces the first occurrence of $regex in $input by the replacement expression $replacement. Example:

echo Regex::replaceFirst("/(\\d\\.\\d)\\.\\d+/", "We support PHP 8.1.26 and 8.2.13.", "$1"); // We support PHP 8.1 and 8.2.3.

MatchResult class

The MatchResult class has three properties:

  • value (string): The matched string.
  • offset (int): The offset of the matched string within the input string.
  • groups (MatchGroup[]): An array of all matched groups.

Groups

The groups property contains an array of MatchGroup objects.

The first element (index 0) will always represent the matched string, therefore the value and offset properties of the MatchGroup object will be the same as the ones of its MatchResult parent.

If the regular expression did not declare any groups, the first element will be the only one in the array.

If the regular expression does declare groups, there will be a MatchGroup object for every group in the order of appearance. Therefore, the first group will have the index 1, the second the index 2, and so on. If there are named groups, there will be additional array elements with the name as index. Note that the MatchGroup object will have the same properties as the one with the numeric index, but the object reference will not be the same.

MatchGroup class

The MatchGroup class has two properties:

  • value (string|null): The matched string or null if the declared group is outside the matched string.
  • offset (int): The offset of the matched string within the input string. -1 if the declared group is outside the matched string.

Usage

Regex::containsMatchIn

use Regex\Regex;

if (Regex::containsMatchIn(pattern: "/^[a-z]+$/", subject: $_GET["input"])) {
    ...
}

Regex::find

use Regex\Regex;

$matchResult = Regex::find(pattern: "/(?P<year>\d{4})\/(?P<month>\d{2})\/(?P<day>\d{2})/", subject: $input);
if ($matchResult !== null) {
    $year = $matchResult->getGroup("year")?->getValue();
    $month = $matchResult->getGroup("month")?->getValue();
    $day = $matchResult->getGroup("day")?->getValue();
}

hoels/regex 适用场景与选型建议

hoels/regex 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 592 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-18