hansott/range-regex

Returns a regex-compatible range from two numbers, min and max.


Keywords
validation, number, regex, numeric, range, Match, glob, hansott, ranges, brace, expansion
License
MIT

Documentation

Range Regex

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Returns a regex-compatible range from two numbers, min and max. Inspired by jonschlinkert/to-regex-range.

Install

Via Composer

$ composer require hansott/range-regex

Usage

use HansOtt\RangeRegex\FactoryDefault;
use HansOtt\RangeRegex\Range;

$factory = new FactoryDefault();
$converter = $factory->getConverter();

$range = new Range(1, 3456);
$regex = sprintf('/^(%s)$/', $converter->toRegex($range));
// /^([1-9]|[1-9][0-9]|[1-9][0-9]{2}|[1-2][0-9]{3}|3[0-3][0-9]{2}|34[0-4][0-9]|345[0-6])$/
$matchesRegex = (bool) preg_match($regex, 0); // false
$matchesRegex = (bool) preg_match($regex, 2014); // true
$matchesRegex = (bool) preg_match($regex, 3457); // false

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Credits

License

The MIT License (MIT). Please see License File for more information.