rafaelnajera/matcher
Composer 安装命令:
composer require rafaelnajera/matcher
包简介
A regexp-like matcher for bespoke tokens.
关键字:
README 文档
README
Matcher implements a regexp-like matching system that can be used with user-devised tokens
Installation
Install the latest version with
$ composer require rafaelnajera/matcher
Usage
The main class is Matcher, which allows you to match a sequence of tokens against a pattern specified in a regexp-like manner.
Matcher works on a Pattern object, which stands for a regexp-like pattern that can be matched.
The following sets up a Pattern object to match '^ab(cd)*e':
$pattern = (new Pattern())->withTokenSeries(['a', 'b']) ->withAddedPatternZeroOrMore((new Pattern())->withTokenSeries(['c', 'd'])) ->withTokenSeries(['e']);
The tokens used to set up the pattern can be of any type. Matching is done
by strict comparison with the input tokens. Tokens can also be objects
that implement the Token interface, in which case the token's matches($someInput)
method will be called. The input in this case can be anything as long as the token's
matches() method knows how to determine a match.
Once set up, a Matcher object can be created and input tokens
can be fed to the it one by one with the match method:
$matcher = new Matcher($pattern); $r = $matcher->match('a'); $r = $matcher->match('b'); ...
Here $r will be false if the input does not match the pattern. $r will be true
if the sequence is still "alive", that is, if the sequence still matches the
pattern in $matcher. When a full match is found the matchFound() method returns
true:
$m = $matcher->matchFound();
The public variable $matcher->matched at this point will contain the actual sequence
of matched tokens or, if tokens implement the Token interface, whatever the
token's matched($someInput) method returns. This array of matched token information
can be manipulated during the matching process with callbacks as explained
below.
The reset() method, resets the internal state of the pattern matcher as if no
token had been fed to it.
$matcher->reset();
Input tokens can also be given in an array:
$r = $matcher->matchArray(['a', 'b', 'c']);
By default this method resets the matcher before starting to match the elements of the given array. An optional flag can be given to change this behaviour:
$r = $matcher->matchArray(['a', 'b', 'c'], false);
Callbacks
A callback can be provided that will be called when a full match occurs. The
callback function is called with $matcher->matched as its only argument and
its output will overwrite $matcher->matched.
The following code, for example, will cause $matcher->matched to be 'abc' instead
of the array ['a', 'b', 'c']:
$pattern = (new Pattern())->withTokenSeries(['a', 'b', 'c']) ->withCallback( function ($m) { return implode($m); } ); $matcher = new Matcher($pattern); $matcher->matchArray(['a', 'b', 'c', 'e']); $matcher->matchFound(); // true $matcher->matched; // 'abc'
Callbacks are retained in their proper places when patterns are added. This allows sub-patterns with specific callbacks to be created. For example:
$subPattern = (new Pattern())->withTokenSeries(['c', 'd']) ->withCallback( function($m) { ... }); $pattern = (new Pattern())->withTokenSeries(['a', 'b']) ->withAddedPatternZeroOrMore($subPattern) ->withTokenSeries(['e']); $matcher = new Matcher($pattern);
In this case, every time the 'cd' subpattern is matched, the callback will be called.
###End Token
The special constant Token::EOF stands for the end of input. It can be used
to set up patterns and also to signal the matcher the end of the input.
$pattern = (new Pattern())->withTokenSeries(['a', 'b', Token::EOF]); $matcher = new Matcher($pattern); $matcher->matchArray(['a', 'b']); // no match $matcher->matchArray(['a', 'b', Token::EOF]); // match found!
Parallel Matching
The class ParallelMatcher matches input tokens against a set
of patterns. Once a match is found in one of the patterns, the matcher
saves that pattern's match sequence and starts matching again. This effectively
considers a valid input stream as a sequence of matched patterns.
The constructor simply takes an array of Pattern objects. Then the match()
method can be called on input tokens as with the Matcher class.
Example:
$pmatcher = new ParallelMatcher( [ (new Pattern())->withTokenSeries(['(', 'a', ')']), (new Pattern())->withTokenSeries(['(', 'b', ')']), (new Pattern())->withTokenSeries(['(', 'c', ')']) ] ); $r = $pmatcher->match('('); // false if the input token does not match any pattern ...
The method matchArray() can be used to match an array of tokens
against the patterns. It returns true if the input sequence was matched
perfectly and false if there was any error. The number of matched patterns
can be found with numMatches()
Example:
$result = $pmatcher->matchArray(['(', 'c', ')', '(', 'c', ')']); // true $pmatcher->numMatches(); // 2 $result = $pmatcher->matchArray(['(', 'a', ')', '(', 'x', ')']); // false $pmatcher->numMatches(); // 1
rafaelnajera/matcher 适用场景与选型建议
rafaelnajera/matcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 91 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「regexp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rafaelnajera/matcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rafaelnajera/matcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rafaelnajera/matcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Emoji regex parser
Universal PHP library for different perposis
A PHP port of https://github.com/francisrstokes/super-expressive
Regular Expression Tool
Symfony bundle for bitcoin address validation
Regexp::Trie in PHP
统计信息
- 总下载量: 91
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-15