phlak/glob

此包已被 废弃 且不再维护。作者建议使用 phlak/splat 包。

类似 glob 的模式匹配和实用工具

维护者

详细信息

github.com/PHLAK/Splat

源代码

问题

资助包维护!
PHLAK
Paypal

5.0.1 2023-03-28 15:56 UTC

README

Splat

Join our Community Become a Sponsor One-time Donation
Latest Stable Version Total Downloads License GitHub branch checks state

类似 glob 的文件和模式匹配工具。

需求

安装

使用 Composer 安装 Splat。

composer require phlak/splat

然后根据需要导入 GlobPattern 类。

use PHLAK\Splat\Glob;
use PHLAK\Splat\Pattern;

模式

Glob 方法接受一个 $pattern 作为第一个参数。这可以是一个字符串或 \PHLAK\Splat\Pattern 的实例。

$pattern = new Pattern(...);
$pattern = Pattern::make(...);

模式字符串可以包含一个或多个以下特殊匹配表达式。

匹配表达式

  • ? 匹配任意单个字符
  • * 匹配零个或多个字符,不包括 / (在 Windows 上为 \
  • ** 匹配零个或多个字符,包括 / (在 Windows 上为 \
  • [abc] 匹配集合中的单个字符(即 abc
  • [a-c] 匹配范围内的单个字符(即 abc
  • [^abc] 匹配集合之外的任意字符(即非 abc
  • [^a-c] 匹配范围之外的任意字符(即非 abc
  • {foo,bar,baz} 匹配集合中的任意模式(即 foobarbaz
    • 集合可以包含其他匹配模式(即 {foo,ba[rz]}

断言

以下断言可用于断言字符串后面或不是后面跟有另一个模式。

  • (=foo) 匹配包含 foo 的任意字符串
  • (!foo) 匹配不包含 foo 的任意字符串

例如,模式 *.tar(!.{gz,xz}) 将匹配以 .tar.tar.bz 结尾的字符串,但不匹配 tar.gztar.xz

将模式转换为正则表达式

Glob 模式可以转换为正则表达式模式。

Pattern::make('foo')->toRegex(); // Returns '#^foo$#'
Pattern::make('foo/bar.txt')->toRegex(); // Returns '#^foo/bar\.txt$#'
Pattern::make('file.{yml,yaml}')->toRegex(); // Returns '#^file\.(yml|yaml)$#'

您可以通过 $options 参数控制正则表达式的行锚。

Pattern::make('foo')->toRegex(Glob::NO_ANCHORS); // Returns '#foo#'
Pattern::make('foo')->toRegex(Glob::START_ANCHOR); // Returns '#^foo#'
Pattern::make('foo')->toRegex(Glob::END_ANCHOR); // Returns '#foo$#'
Pattern::make('foo')->toRegex(Glob::BOTH_ANCHORS); // Returns '#^foo$#'
Pattern::make('foo')->toRegex(Glob::START_ANCHOR | Glob::END_ANCHOR); // Returns '#^foo$#'

模式字符转义

有时字符串中可能包含不应视为匹配表达式字符的字符。在这些情况下,您可以通过在前面加上反斜杠(\)来转义任何字符。

Pattern::make('What is happening\?');
Pattern::make('Wall-E \[2008\].mp4');

您还可以使用 Pattern::escape() 方法在程序中转义字符串中的 glob 模式字符。

Pattern::escape('What is happening?'); // Returns 'What is happening\?'
Pattern::escape('*.{yml,yaml}'); // Returns '\*.\{yml\,yaml\}'
Pattern::escape('[Ss]pl*t.txt'); // Returns '\[Ss\]pl\*t.txt'

方法

目录中的文件

获取匹配 glob 模式的目录中的文件列表。

Glob::in('**.txt', 'some/file/path');

返回一个包含指定目录内匹配glob模式的文件的Symfony Finder组件(例如 foo.txtfoo/bar.txtfoo/bar/baz.txt 等)。

精确匹配

测试字符串是否匹配glob模式。

Glob::match('*.txt', 'foo.txt'); // true
Glob::match('*.txt', 'foo.log'); // false

匹配开始

测试字符串是否以glob模式开头。

Glob::matchStart('foo/*', 'foo/bar.txt'); // true
Glob::matchStart('foo/*', 'bar/foo.txt'); // false

匹配结束

测试字符串是否以glob模式结尾。

Glob::matchEnd('**.txt', 'foo/bar.txt'); // true
Glob::matchEnd('**.txt', 'foo/bar.log'); // false

匹配其中

测试字符串是否包含glob模式。

Glob::matchWithin('bar', 'foo/bar/baz.txt'); // true
Glob::matchWithin('bar', 'foo/baz/qux.txt'); // false

过滤字符串数组

过滤字符串数组以匹配glob模式的值。

Glob::filter('**.txt', [
    'foo', 'foo.txt', 'bar.zip', 'foo/bar.png', 'foo/bar.txt',
]);

// Returns ['foo.txt', 'foo/bar.txt']

拒绝字符串数组

过滤字符串数组以匹配不匹配glob模式的值。

Glob::reject('**.txt', [
    'foo', 'foo.txt', 'bar.zip', 'foo/bar.png', 'foo/bar.txt',
]);

// Returns ['foo', 'bar.zip', 'foo/bar.png']

变更日志

可以在GitHub发行页面找到变更列表。

故障排除

为了获取一般帮助和支持,请加入我们的GitHub讨论或在Twitter上联系。

请向GitHub问题跟踪器报告错误。

版权

本项目采用MIT许可证授权。