Set of regular expressions.
ldrahnik/regexp requires PHP 5.4 or higher.
Install regexp to your project using Composer:
$ composer require ldrahnik/regexp
Register extension in config file
extensions:
regexp: regexp\DI\RegexpHelperExtension
regexp:
myRegularName: ^[0-9]{1,10}$
twitterUsername: foo
Now you can use all regulars through services
/** @var \regexp\Regexp @inject */
private $regexp;
public function __construct(regexp\Regexp $regexp)
{
$this->regexp = $regexp;
}
public function createComponentForm()
{
$form = new Nette\Application\UI\Form();
$form->addText('twitter', 'Twitter username')
->setDefaultValue('@')
->addCondition(Form::FILLED)
->addRule(Form::PATTERN, 'Please enter twitter username, for example: @username',
$this->regexp->getTwitterUsername());
...
// $this->regexp->getRegularExpression('twitterUsername'));
// equivalent of that expression is
// $this->regexp->getTwitterUsername();
}
....
You are able to use or override already existing embedded regular expressions
'username' => '^[a-z0-9_-]{3,16}$',
'twitterUsername' => '^(\@)?[A-Za-z0-9_]+$',
'password' => '^[a-z0-9_-]{6,18}$',
'facebook' => '^(https?:\/\/)?(www\.)?facebook.com\/[a-zA-Z0-9(\.\?)?]',
'google' => '((http|https):\/\/)?(www[.])?plus\.google\.com\/.?\/?.?\/?([0-9]*)'