markwilson / verbal-expressions-php
此包已被 废弃 并不再维护。未建议替代包。
PHP 版本的 VerbalExpressions;从 jehna/VerbalExpressions 转移而来
0.1.0
2013-07-24 11:37 UTC
Requires (Dev)
- phpunit/phpunit: 3.7.*
- squizlabs/php_codesniffer: 1.*
This package is not auto-updated.
Last update: 2021-01-23 09:26:24 UTC
README
PHP 版本的 jehna/VerbalExpressions。
安装
Composer
将以下内容添加到 composer.json 文件中:
{ "require": { ..., "markwilson/verbal-expressions-php": "dev-master" } }
示例用法
<?php require_once 'vendor/autoload.php'; use MarkWilson\VerbalExpression; use MarkWilson\VerbalExpression\Matcher; // initialise verbal expression instance $verbalExpression = new VerbalExpression(); // URL matcher $verbalExpression->startOfLine() ->then('http') ->maybe('s') ->then('://') ->maybe('www.') ->anythingBut(' ') ->endOfLine(); // compile expression - returns ^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$ $verbalExpression->compile(); // perform match preg_match($verbalExpression, 'http://www.google.com'); // returns 1 // or $matcher = new Matcher(); $matcher->isMatch($verbalExpression, 'http://www.google.com'); // returns true
嵌套表达式
<?php $innerExpression = new VerbalExpression(); $innerExpression->word(); $outerExpression = new VerbalExpression(); $outerExpression->startOfLine() ->find($innerExpression) ->then($innerExpression) ->endOfLine(); // returns ^(\w+)(\w+)$ $outerExpression->compile();
禁用子模式捕获
默认情况下,子模式会被捕获并在匹配数组中返回。
<?php // disable sub pattern capture $verbalExpression->disableSubPatternCapture()->word(); // (?:\w+) // or $verbalExpression->word(false); // (?:\w+)
禁用此功能将仅影响后续添加到表达式的部分;已添加的部分将不受影响。这允许在组内启用和禁用。
<?php // equivalent to (\w+)(?:\w+)(?:\w+)(\w+) $verbalExpression->word() ->disableSubPatternCapture() ->word() ->word() ->enableSubPatternCapture() ->word();