-
Notifications
You must be signed in to change notification settings - Fork 2
Installation, How to Use
pip install oprex
>>> 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.
$ 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
- IPv4 Address
- BEGIN-something-END
- Date
- Time
- Blood Type
- Quoted String
- Comma-Separated Values
- Password Checks
- Balanced Parentheses
- Number-string Range Literal
- Backreference
- Flags
- Match-anything-until
- Recursion
- Global Variables
- Anchors
- Built-in Character Classes
- Built-in Expressions
-
Special Built-ins:
WOB
,wordchar
,non-linechar