phlak / splat
类似Glob的模式匹配和实用工具
Requires
- php: >=8.0
- symfony/finder: ^6.0
Requires (Dev)
- phlak/coding-standards: ^2.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.0
- psy/psysh: ^0.11
- symfony/var-dumper: ^6.0
- yoast/phpunit-polyfills: ^1.0
This package is auto-updated.
Last update: 2024-08-30 01:07:11 UTC
README
类似Glob的文件和模式匹配实用工具。
要求
- PHP >= 8.0
安装
使用Composer安装Splat。
composer require phlak/splat
然后根据需要导入Glob
或Pattern
类。
use PHLAK\Splat\Glob; use PHLAK\Splat\Pattern;
模式
Glob
方法接受一个$pattern
作为第一个参数。这可以是一个字符串或\PHLAK\Splat\Pattern
的一个实例。
$pattern = new Pattern(...); $pattern = Pattern::make(...);
模式字符串可以包含以下特殊匹配表达式之一或多个。
匹配表达式
?
匹配任意单个字符*
匹配零个或多个字符(不包括/
(在Windows上为\
))**
匹配零个或多个字符(包括/
(在Windows上为\
))[abc]
匹配集合中的单个字符(即a
、b
或c
)[a-c]
匹配范围中的单个字符(即a
、b
或c
)[^abc]
匹配不在集合中的任意字符(即不是a
、b
或c
)[^a-c]
匹配不在范围中的任意字符(即不是a
、b
或c
){foo,bar,baz}
匹配集合中的任意模式(即foo
、bar
或baz
)- 集合可以包含其他匹配模式(例如,
{foo,ba[rz]}
)
- 集合可以包含其他匹配模式(例如,
断言
以下断言可用于断言字符串是否紧跟或紧随另一个模式。
(=foo)
匹配包含foo
的任意字符串(!foo)
匹配不包含foo
的任意字符串
例如,模式*.tar(!.{gz,xz})
将匹配以.tar
或.tar.bz
结尾的字符串,但不匹配tar.gz
或tar.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模式的文件的指定目录(例如,foo.txt
、foo/bar.txt
、foo/bar/baz.txt
等)的Symfony Finder组件。
精确匹配
测试字符串是否与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 许可协议。