Python rex is regular expressions for humans. (rex is also abbreviation from re X tended).
It is for the re standard module like requests is for urllib module.
Rex also is latin for "king", and the king of regular expressions is Perl. So rex API tries to mimic at least some Perl's idioms.
Installation ===========
pip install python-rex
or
pip install -e git+https://github.com/cypreess/python-rex.git#egg=rex-dev
There are no external dependencies.
from rex import rex
So far rex supports:
- simple matching (first match),
- substitution,
- all python re flags.
The most obvious usage - test condition by matching to string:
if 'This is a dog' == rex('/dog/'):
print 'Oh yeah'
or:
if 'My lucky 777 number' == rex('/[0-9]+/'):
print 'Number found'
You can use Perl notation and prepend m
character to your search:
if 'My lucky 777 number' == rex('m/[0-9]+/'):
print 'Number found'
but you can also simply check your much:
if ('My lucky 777 number' == rex('m/[0-9]+/'))[0] == '777':
print 'Number found'
or even or even groups:
if ('My lucky 777 number' == rex('m/(?P<number>[0-9]+)/'))['number'] == '777':
print 'Number found'
Don't worry if you don't find anything, it won't brake:
>> print ('My lucky 777 number' == rex('m/(?P<number>[0-9]+)/'))['no_such_keyword']
None
Substitution can be made prepending pattern with s
character (like in perl expression):
>>> print "This is a cat" == rex('s/CAT/dog/i')
This is a dog