
Métodos irregulares em expressões regulares
Métodos irregulares para expressões regulares.
Exrex é uma ferramenta de linha de comando e um módulo python que gera todas - ou aleatórias - strings correspondentes a uma expressão regular e mais. É python puro, sem dependências externas.
Existem expressões regulares com strings correspondentes infinitas (ex.: [a-z]+), nesses casos o exrex limita o comprimento máximo das partes infinitas.
Exrex usa geradores, então o uso de memória não depende do número de strings correspondentes.
Características
Para instalar o exrex, simplesmente:
$ pip install exrex
ou
$ easy_install exrex
>>> import exrex
>>> exrex.getone('(ex)r\\1')
'exrex'
>>> list(exrex.generate('((hai){2}|world!)'))
['haihai', 'world!']
>>> exrex.getone('\d{4}-\d{4}-\d{4}-[0-9]{4}')
'3096-7886-2834-5671'
>>> exrex.getone('(1[0-2]|0[1-9])(:[0-5]\d){2} (A|P)M')
'09:31:40 AM'
>>> 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.
>>> print exrex.simplify('(ab|ac|ad)')
(a[bcd])
> exrex --help
usage: exrex.py [-h] [-o FILE] [-l] [-d DELIMITER] [-v] REGEX
exrex - gerador de strings de expressão regular
argumentos posicionais:
REGEX String REGEX
argumentos opcionais:
-h, --help mostra esta mensagem de ajuda e sai
-o FILE, --output FILE
Arquivo de saída - padrão é STDOUT
-l N, --limit N Limite máximo para tamanho do intervalo - padrão é 20
-c, --count Contar strings correspondentes
-m N, --max-number N Número máximo de strings - padrão é -1
-r, --random Retorna uma string aleatória que corresponde à regex
-s, --simplify Simplifica uma expressão regular
-d DELIMITER, --delimiter DELIMITER
Delimitador - padrão é \n
-v, --verbose Modo verboso
Exemplos:
$ exrex '[asdfg]'
a
s
d
f
g
$ exrex -r '(0[1-9]|1[012])-\d{2}'
09-85
$ exrex '[01]{10}' -c
1024
Bugs ou sugestões? Visite o rastreador de problemas.
http://exrex.readthedocs.org/en/latest/
re.sre_parse.CATEGORIES) (30%)n strings correspondentes aleatórias diferentesexrex 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, <[email protected]>
exrex '( {20}(\| *\\|-{22}|\|)|\.={50}| ( ){0,5}\\\.| {12}~{39})'exrex '(o|O|0)(_)(o|O|0)'Ferramentas que geram uma lista de todas as strings possíveis que correspondem a um determinado padrão:
Ferramentas que geram strings aleatórias, uma por uma, que correspondem a um determinado padrão:
rstr)python -m cProfile exrex.py '[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]' -o /dev/nullpython -m cProfile exrex.py '[0-9]{6}' -o /dev/null