Skip to content

Installation, How to Use

Ron Panduwana edited this page Sep 27, 2015 · 3 revisions

Installation

pip install oprex

Usage

>>> from oprex import oprex
>>> oprex
<function oprex at ...>

The oprex function takes a parameter of type string (the oprex "source code") with the return value also of type string (the resulting regex).

>>> email_pattern = oprex('''
...     /somebody/at/somedomain/
...         somebody = @1.. of: alnum . _ - +
...         at = '@'
...         somedomain = /subdomain/TLD/
...             subdomain = /hostname/dot/subdomain?/
...                 hostname = <@>
...                     |!dash>
...                     |alnums_and_dashes|
...                                 <!dash|
...                 
...                         dash: -
...                         alnums_and_dashes = @1.. of: alnum dash
...                 dot = '.'
...             TLD = @2..63 of alpha
... ''')
>>> print email_pattern
(?V1w)[a-zA-Z0-9._\-+]++@(?P<subdomain>(?!-)[a-zA-Z0-9\-]++(?<!-)\.(?&subdomain)?)[a-zA-Z]{2,63}+

The output is designed to be used with Matthew Barnett's regex module. If you want to use it with Python's built-in re module, you'll need to remove the V1 flag (in addition to not using the features re does not support, of course).

Since the output is plain regular python string, such modification (or any other string manipulation you may want) should be pretty straightforward.

As command line tool

$ python oprex.py <sourcefile>

You can use oprex from command line. It takes path-to-source-file as a parameter. The file should be in plaintext, containing oprex "source code". For consistency with embedded-in-python-triple-quote-string syntax, the first and last lines of the file should be blank. Example:


./palindrome/.
    palindrome = <<|
                   |/letter/palindrome/=letter/
                   |/letter/=letter/
                   |letter

        [letter] = alpha
        

(notice that the first and last lines are blanks)

The output will be printed on stdout.

$ python oprex.py samples/palindrome.oprex
(?V1w)\A(?P<palindrome>(?P<letter>[a-zA-Z])(?&palindrome)(?P=letter)|(?P<letter>[a-zA-Z])(?P=letter)|(?P<letter>[a-zA-Z]))\Z
Clone this wiki locally