The Hoa\Regex 库。

v2.0.0 2022-12-17 12:24 UTC

README

Hoa

Build status Code coverage Packagist License

Hoa 是一组 模块化可扩展结构化 的 PHP 库。
此外,Hoa 致力于成为工业界和学术界之间的桥梁。

Hoa\Regex

Help on IRC Help on Gitter Documentation Board

这个库提供了分析正则表达式和根据正则表达式生成字符串的工具(Perl 兼容正则表达式)。

了解更多.

安装

使用 Composer,要将此库包含到依赖项中,您需要添加 hoa/regex

$ composer require hoa/regex '~1.0'

有关更多安装说明,请参阅 源代码页面

测试

在运行测试套件之前,必须安装开发依赖项

$ composer install

然后,要运行所有测试套件

$ vendor/bin/hoa test:run

有关更多信息,请参阅 贡献者指南

快速使用

作为一个快速概述,我们建议查看两个示例。首先,分析一个正则表达式,即 lex、解析和生成 AST。其次,通过使用各向同性的随机方法遍历其 AST 来根据正则表达式生成字符串。

分析正则表达式

我们需要 Hoa\Compiler 来分析以下正则表达式 ab(c|d){2,4}e? 的词法、语法和生成 AST。因此

// 1. Read the grammar.
$grammar  = new Hoa\File\Read('hoa://Library/Regex/Grammar.pp');

// 2. Load the compiler.
$compiler = Hoa\Compiler\Llk\Llk::load($grammar);

// 3. Lex, parse and produce the AST.
$ast      = $compiler->parse('ab(c|d){2,4}e?');

// 4. Dump the result.
$dump     = new Hoa\Compiler\Visitor\Dump();
echo $dump->visit($ast);

/**
 * Will output:
 *     >  #expression
 *     >  >  #concatenation
 *     >  >  >  token(literal, a)
 *     >  >  >  token(literal, b)
 *     >  >  >  #quantification
 *     >  >  >  >  #alternation
 *     >  >  >  >  >  token(literal, c)
 *     >  >  >  >  >  token(literal, d)
 *     >  >  >  >  token(n_to_m, {2,4})
 *     >  >  >  #quantification
 *     >  >  >  >  token(literal, e)
 *     >  >  >  >  token(zero_or_one, ?)
 */

我们读取到整个表达式由两个标记的组合组成: ab,然后是量词,然后是另一个量词。第一个量词是两个标记(即 cd 之间的选择)之间的交替,2 到 4 次。第二个量词是 e 标记,可以出现零次或一次。

我们可以借助 Hoa\Visitor 遍历树。

根据正则表达式生成字符串

要生成基于正则表达式 AST 的字符串,我们将使用 Hoa\Regex\Visitor\Isotropic 访问者

$generator = new Hoa\Regex\Visitor\Isotropic(new Hoa\Math\Sampler\Random());
echo $generator->visit($ast);

/**
 * Could output:
 *     abdcde
 */

字符串随机生成并匹配给定的正则表达式。

文档

Hoa\Regex 的黑客手册 包含有关如何使用此库及其工作原理的详细信息。

要本地生成文档,请执行以下命令

$ composer require --dev hoa/devtools
$ vendor/bin/hoa devtools:documentation --open

更多文档可以在项目网站上找到: hoa-project.net

获取帮助

主要有两种方式可以获得帮助

贡献

你想贡献力量吗?谢谢!一个详细的 贡献者指南 解释了你需要知道的一切。

许可

华奥项目遵循新BSD许可证(BSD-3-Clause)。请参阅许可证获取详细信息。