shopbase/regex

此包已被废弃,不再维护。作者建议使用https://github.com/flexicsystems/regex包。

这是一个允许在不进行太多设置的情况下使用PHP正则表达式的包。你可以像使用默认的preg_*函数一样使用静态函数,并且它们会返回一个带有许多功能的匹配对象。

1.1.0.0 2019-01-13 17:40 UTC

This package is auto-updated.

Last update: 2022-11-04 11:21:06 UTC


README

Regex

这是一个允许在不进行太多设置的情况下使用PHP正则表达式的包。你可以像使用默认的preg_*函数一样使用静态函数,并且它们会返回一个带有许多功能的匹配对象。

版权(c)2019 Shopbase

使用

有几个函数可以使用。

// preg_match
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz');

// preg_match_all
$result = \Shopbase\Regex\Regex::matchAll('/(?J)(?<match>foo)|(?<match>bar)/', 'foo bar');

// preg_replace
$result = \Shopbase\Regex\Regex::replace('/(\d+)\. (\w+) (\d+)/i', '15. April 2003', '${2}1,$3');

// preg_split
$result = \Shopbase\Regex\Regex::split('/[\s,]+/', 'hypertext language, programming');

// preg_grep
$result = \Shopbase\Regex\Regex::grep('/^(\d+)?\.\d+$/', array(0,1,0.5,1.5));

// preg_filter
$result = \Shopbase\Regex\Regex::filter(array('/\d/', '/[a-z]/', '/[1a]/'), array('A:$0', 'B:$0', 'C:$0'), array('1', 'a', '2', 'b', '3', 'A', 'B', '4'));

// preg_quote. This function will validate you expression and return it as string.
\Shopbase\Regex\Regex::validateExpression('...');
\Shopbase\Regex\Regex::quote('...');

所有这些函数都会返回类型为 '\Shopbase\Regex\Result\Result' 的对象。该对象包含匹配项及其相关信息。

// get the boolean status if the result contains matches or not.
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getHasMatches();

// get the count of contained matches
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getCount();

// get the '\Shopbase\Regex\Result\Matches' object. This object contains all found matches.
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches();

如果你使用Result对象的'getMatches'函数,你将得到一个包含所有匹配项及其处理函数列表的对象。

// get array with all matches
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->matches();

// get a group by int. Here it is possible to include subgroups (Every array item will represent an group into an group)
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->group(0);
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->group(0, [0, 0]);

// you can do the same group function with string values. It will work equal as the 'group' function
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->namedGroup('group');
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->namedGroup('group', ['subgroup', 'subgroup']);

// to get multiple groups you can use the 'groups' function. Here it is possible to include the subgroups of every item, too. 
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->groups(0, 1, 2);
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->groups([0, 0], [1, 0], 2);

$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->namedGroups('group_1', 'group_2', 'group_3');
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->namedGroups(['group_1', 'subgroup_1'], ['group_2', 'subgroup_1', 'subgroup_2'], 'group_3');

// map matches by callback function
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->map(function ($item){...});

// merge matches to string
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->merge();

// get count of matches
$result = \Shopbase\Regex\Regex::match('/(foo)(bar)(baz)/', 'foobarbaz')->getMatches()->count();

regex函数可以接受几个标志。这些标志包含在以下对象中。

\Shopbase\Regex\RegexFlags::$PREG_OFFSET_CAPTURE;
\Shopbase\Regex\RegexFlags::$PREG_UNMATCHED_AS_NULL;
\Shopbase\Regex\RegexFlags::$PREG_PATTERN_ORDER;
\Shopbase\Regex\RegexFlags::$PREG_SET_ORDER;
\Shopbase\Regex\RegexFlags::$PREG_GREP_INVERT;
\Shopbase\Regex\RegexFlags::$PREG_SPLIT_NO_EMPTY;
\Shopbase\Regex\RegexFlags::$PREG_SPLIT_DELIM_CAPTURE;
\Shopbase\Regex\RegexFlags::$PREG_SPLIT_OFFSET_CAPTURE;

模式

该包包含一个对象,其中包含一些预定义的模式

\Shopbase\Regex\RegexPattern::getHtmlSearchExpression('div');

Donate