exrex/README.md

184 lines
4.6 KiB
Markdown
Raw Normal View History

2012-05-27 11:13:45 +00:00
EXREX
=====
2014-07-08 18:09:15 +00:00
Irregular methods for regular expressions.
Exrex is a command line tool and python module that generates all - or random - matching strings to a given regular expression and more.
It's pure python, without external dependencies.
2012-09-23 21:19:48 +00:00
2012-09-29 14:32:49 +00:00
There are regular expressions with infinite matching strings (eg.: `[a-z]+`), in these cases exrex limits the maximum length of the infinite parts.
2012-05-27 11:13:45 +00:00
2013-03-24 20:33:41 +00:00
Exrex uses generators, so the memory usage does not depend on the number of matching strings.
[![Version](https://img.shields.io/pypi/v/exrex.svg)](https://crate.io/packages/exrex/) [![Downloads](https://img.shields.io/pypi/dm/exrex.svg)](https://crate.io/packages/exrex/)
2013-09-19 13:29:58 +00:00
*Features*
2013-04-02 23:16:02 +00:00
2015-08-31 12:35:44 +00:00
* Generating all matching strings
* Generating a random matching string
* Counting the number of matching strings
2014-07-08 18:09:15 +00:00
* Simplification of regular expressions
### Installation
To install exrex, simply:
```bash
$ pip install exrex
```
or
```bash
$ easy_install exrex
```
2012-07-04 13:53:44 +00:00
Usage
2013-04-05 11:40:33 +00:00
=====
### as python module
2012-09-24 13:10:08 +00:00
```python
>>> import exrex
2014-03-14 13:38:42 +00:00
>>> exrex.getone('(ex)r\\1')
'exrex'
>>> list(exrex.generate('((hai){2}|world!)'))
['haihai', 'world!']
2012-09-24 13:10:08 +00:00
>>> exrex.getone('\d{4}-\d{4}-\d{4}-[0-9]{4}')
'3096-7886-2834-5671'
2013-04-02 22:03:21 +00:00
>>> exrex.getone('(1[0-2]|0[1-9])(:[0-5]\d){2} (A|P)M')
'09:31:40 AM'
2012-09-24 13:10:08 +00:00
>>> exrex.count('[01]{0,9}')
1023
>>> print '\n'.join(exrex.generate('This is (a (code|cake|test)|an (apple|elf|output))\.'))
This is a code.
This is a cake.
This is a test.
This is an apple.
This is an elf.
This is an output.
2014-07-08 18:09:15 +00:00
>>> print exrex.simplify('(ab|ac|ad)')
(a[bcd])
2012-09-24 13:10:08 +00:00
```
2012-05-27 11:13:45 +00:00
### Command line usage
```
2013-11-07 17:42:12 +00:00
> exrex --help
2012-07-04 10:28:47 +00:00
usage: exrex.py [-h] [-o FILE] [-l] [-d DELIMITER] [-v] REGEX
2012-05-27 11:13:45 +00:00
exrex - regular expression string generator
positional arguments:
REGEX REGEX string
optional arguments:
-h, --help show this help message and exit
-o FILE, --output FILE
Output file - default is STDOUT
2013-07-23 15:01:28 +00:00
-l N, --limit N Max limit for range size - default is 20
2012-07-10 10:15:31 +00:00
-c, --count Count matching strings
2013-07-23 15:01:28 +00:00
-m N, --max-number N Max number of strings - default is -1
2012-09-23 21:19:48 +00:00
-r, --random Returns a random string that matches to the regex
2014-07-08 18:31:54 +00:00
-s, --simplify Simplifies a regular expression
2012-05-27 11:13:45 +00:00
-d DELIMITER, --delimiter DELIMITER
Delimiter - default is \n
2012-07-04 10:28:47 +00:00
-v, --verbose Verbose mode
```
Examples:
2013-04-30 01:58:13 +00:00
2012-07-04 10:28:47 +00:00
```
2013-11-07 17:42:12 +00:00
$ exrex '[asdfg]'
2012-07-04 10:28:47 +00:00
a
s
d
f
g
2012-07-10 10:38:01 +00:00
2013-11-07 17:42:12 +00:00
$ exrex -r '(0[1-9]|1[012])-\d{2}'
2012-09-24 13:10:08 +00:00
09-85
2012-07-10 10:38:01 +00:00
2013-11-07 17:42:12 +00:00
$ exrex '[01]{10}' -c
2012-09-24 13:10:08 +00:00
1024
2013-04-30 01:58:13 +00:00
2012-05-27 11:13:45 +00:00
```
### Bugs
Bugs or suggestions? Visit the [issue tracker](https://github.com/asciimoo/exrex/issues).
### Documentation
2013-04-05 11:40:33 +00:00
http://exrex.readthedocs.org/en/latest/
### TODO
2012-05-27 11:13:45 +00:00
2013-08-23 08:23:55 +00:00
* Command line switches to change default character sets/ranges/range limits (eg. for '.','\s'..) (40%)
* Extend categories (`re.sre_parse.CATEGORIES`) (30%)
* Improve setup.py
* More verbose code
* Documentation
* Optimizations
* Generation of `n` different random matching string
2013-04-02 22:03:21 +00:00
* Memory usage reduction (100%?) - generators
2013-04-09 11:11:23 +00:00
* Count the number of matching strings - (100%?)
2013-04-09 14:19:20 +00:00
* Unicode support (100%)
2013-04-30 01:58:13 +00:00
* Handle grouprefs (100%)
2013-05-13 09:48:47 +00:00
* Python3 compatibility (100%) ( >= python3.3)
2012-05-27 11:13:45 +00:00
### License
2012-05-27 11:13:45 +00:00
```
exrex is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
exrex is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with exrex. If not, see < http://www.gnu.org/licenses/ >.
(C) 2012- by Adam Tauber, <asciimoo@gmail.com>
```
### Fun/arts
2012-05-27 11:13:45 +00:00
2013-11-07 17:42:12 +00:00
* Boat: `exrex '( {20}(\| *\\|-{22}|\|)|\.={50}| ( ){0,5}\\\.| {12}~{39})'`
* Eyes: `exrex '(o|O|0)(_)(o|O|0)'`
2012-07-10 13:55:50 +00:00
### Similar projects
Tools that generate a list of all possible strings that match a given pattern:
* [regldg](http://regldg.com/)
(features a live demo on the website)
* [regex-genex](https://github.com/audreyt/regex-genex)
(supports using multiple regex patterns simultaneously)
Tools that generate random strings, one by one, that match a given pattern:
* [randexp.js](http://fent.github.com/randexp.js/)
(features several live demos on the website)
* [rstr.xeger](https://bitbucket.org/leapfrogdevelopment/rstr/src#rst-header-xeger)
(a method of the `rstr` Python module)
2013-03-24 20:33:41 +00:00
### Profiling
2012-07-10 13:55:50 +00:00
* `python -m cProfile exrex.py '[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]' -o /dev/null`
* `python -m cProfile exrex.py '[0-9]{6}' -o /dev/null`