ldrahnik/regexp

Set of regular expressions.

v1.3 2015-04-14 21:49 UTC

This package is not auto-updated.

Last update: 2024-12-21 18:19:22 UTC


README

Build Status Latest stable Downloads total

Set of regular expressions.

Requirements

ldrahnik/regexp requires PHP 5.4 or higher.

Installation

Install regexp to your project using Composer:

$ composer require ldrahnik/regexp

Usage

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]*)'