regex_range 0.0.1+2 regex_range: ^0.0.1+2 copied to clipboard
Generates a Regular Expression that matches a range of numbers or characters. Use the generated RegExp or pattern.
Regex Range #
Generates a Regular Expression that matches a range of numbers or characters.
Features #
- Number ranges:
1 -> 100
- Negative to positive number ranges:
-100 -> 100
- Character ranges:
A -> Z
- Case insensitive character ranges:
a -> Z
Getting Started #
dart pub add regex_range
And import the package:
import 'package:regex_range/regex_range.dart';
Usage #
To generate a regex, you may use the range
function to generate both number and character ranges, depending on what's passed in.
A number range is generated when passing in 2 numbers, and a character range is generated when passing in 2 strings with a length of 1.
Alternatively, you may use the numberRange
and charRange
functions to generate a number or character range, respectively.
Expression Pattern #
You can get the generated expression pattern by using the pattern
property of the RegExp
object.
final regex = range(0, 5);
print(regex.pattern); // [0-5]
You may also use the exact
parameter to generate an exact match, this will add ^
and $
to the expression to ensure it matches as a whole.
Number Range #
final regex = range(-10, 10, exact: true);
// or
final regex = numberRange(-10, 10, exact: true);
print(regex.hasMatch('-5')); // true
print(regex.hasMatch('5')); // true
print(regex.hasMatch('11')); // false
Character Range #
Case sensitivity is disabled when passing 2 characters with a different case. Case sensitivity can be set manually with the caseSensitive
parameter in the charRange
function.
final regex = range('b', 'Y', exact: true);
// or
final regex = charRange('B', 'Y', caseSensitive: false, exact: true);
print(regex.hasMatch('b')); // true
print(regex.hasMatch('Y')); // true
print(regex.hasMatch('Z')); // false
Generated Regex #
The expressions below are not the exact expressions generated by the package, they are the simplified and equivalent.
Number Range #
Range | Expression |
---|---|
0, 5 | [0-5] |
0, 10 | [0-9]|10 |
25, 100 | 2[5-9]|[3-9][0-9]|100 |
-10, 10 | -?[0-9]|-?10 |
Character Range #
Range | Expression |
---|---|
A, Z | [A-Z] |
a, z | [a-z] |
a, Z | [a-zA-Z] |
b, Y | [b-yB-Y] |
Contributing #
Contributions are welcome! Please open an issue or pull request if you find a bug or have a feature request.