braincrafted / expexp
Installs: 20
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 1
Open Issues: 0
pkg:composer/braincrafted/expexp
Requires (Dev)
- phpunit/phpunit: 3.7.*
README
ExpExp expands expressions. That's kinda the opposite of what regular expressions do.
For example ab(cd|[xy]) expands to
abcd,abxandaby.
Author
Features
The following expressions can be expanded by the library:
Disjunction:
abc[xyz]
will be expanded to
abcxabcyabcz
Named character classes:
Instead of listing all disjunct characters, you can also select from a set of available character classes:
uppercontains uppercase characters (from ASCII)lowercontains lowercase characters (from ASCII)digitcontains digitsspacecontains space characterspunctcontains punctuation characters
You can use named character classes by wrapping them in colons:
[:upper:]
Dot Operator:
abc.
will be expanded to
abcAabcB- …
The Dot opterator does not expand to every character, but only to A-Za-z0-9_.
Parantheses:
ab(c)
will be expanded to
abc
Repetition:
The repetition operator allows to repeat the previous character(s). If only one value is given the previous character is repeated that often, if two values are given the character is multiplied with each value in the given range. {,3} is the same as {0,3}.
a{3}
will expand to
aaa
Or with a minimum and a maximum value:
a{1,3}
will expand to
aaaaaa
This also works with disjunctions and parentheses.
Alternation:
abc|xyz
will be expanded to
abcxyz
Optional:
abc?
will be expanded to
abcab
This also works with parantheses:
abc(xyz)?
will be expanded to
abcabcxyz
The optional operator has thus the same effect as {0,1}.
More examples
| Pattern | Count | Expansion |
|---|---|---|
abc |
1 | abc |
ab(c) |
1 | abc |
[abc] |
3 | a, b, c |
a{3} |
1 | aaa |
a{} |
1 | a |
a{1,3} |
3 | a, aa, aaa |
a{,3} |
4 | , a, aa, aaa |
a(bc){2} |
1 | abcbc |
a(bc){1,2} |
2 | abcbc, abc |
a(bc){,2} |
3 | a, abc, abcbc |
[ab]{2} |
2 | aa, bb |
ab. |
63 | abA, abB, aba, ab0, ab_, ... |
abc|xyz |
2 | abc, xyz |
a|b|c |
3 | a, b, c |
ab(c|d) |
2 | abc, abd |
ab(cde|[xyz]) |
4 | abcde, abx, aby, abz |
abc? |
2 | abc, ab |
abc(xyz)? |
2 | abc, abcxyz |
Usage
Instantiate the object and call the expand() method with the pattern:
use Bc\ExpExp\ExpExp;
$e = new ExpExp();
$result = $e->expand('abc|xyz');
More examples can be found in the test cases.
Changelog
Version 0.2.2 (2013-10-20)
- Dot operator matches
wordcharacter class
Version 0.2.1 (2013-10-19)
- Named character classes
Version 0.2 (2013-10-19)
- Changed namespace to
Braincrafted - Added repetition operator
{} - Completely rewritten to be easier and better extensible
- Improved test suite
Version 0.1.1 (2013-10-16)
- Better code style
- Better in-code documentation
Version 0.1 (2013-10-16)
- Moved to
Bcnamespace - Call
expand()with pattern - Better documentation
License
ExpExp is licensed under The MIT License. See the LICENSE file in the projects root directory for more information.