Regex was a mistake. A human-readable regex builder for Python.
Project description
zeroReg
Regex was a mistake. Just like your ex.
Write regex without the tears or confusion.
A human-readable regex builder for Python.
The Problem
import re
pattern = re.compile(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
Your coworker asks what it does. You mass tears. You mass confusion. You mass quit.
The Solution
from zeroreg.patterns import email
email.test('hello@world.com') # True
Or build your own:
from zeroreg import digit, optional
phone = (
optional('+')
.then(digit(3))
.then('-')
.then(digit(3))
.then('-')
.then(digit(4))
)
phone.test('123-456-7890') # True
phone.test('+123-456-7890') # True
phone.to_regex() # re.compile(r'\+?\d{3}-\d{3}-\d{4}')
Installation
pip install zeroreg
API
Character Classes
| Function | Description | Regex |
|---|---|---|
digit(n?) |
Match digits | \d or \d{n} |
word() |
Word characters | \w |
letter() |
Letters only | [a-zA-Z] |
whitespace() |
Whitespace | \s |
any_char() |
Any character | . |
literal(str) |
Exact match | (escaped) |
char_in(chars) |
Match any in set | [...] |
char_not_in(chars) |
Match any NOT in set | [^...] |
Quantifiers
| Method | Description |
|---|---|
.one_or_more() |
1+ times |
.zero_or_more() |
0+ times |
.optional() |
0 or 1 |
.times(n) |
Exactly n |
.between(min, max) |
Range |
.at_least(n) |
n or more |
Groups
| Function | Description |
|---|---|
capture(pattern, name?) |
Capturing group |
group(pattern) |
Non-capturing group |
one_of(*patterns) |
Match any of |
Anchors
| Function | Description |
|---|---|
start_of_line() |
^ |
end_of_line() |
$ |
word_boundary() |
\b |
Output
| Method | Description |
|---|---|
.to_regex(flags?) |
Get compiled re.Pattern |
.test(str) |
Test string |
.match(str) |
Get match object |
.match_all(str) |
Get all matches |
.replace(str, replacement) |
Replace matches |
Pre-built Patterns
from zeroreg.patterns import email, url, phone, date, ipv4, uuid
Available: email, url, phone, date, time, ipv4, ipv6, hex_color, hex_string, uuid, slug, hashtag, mention, credit_card, ssn, zip_code, username, strong_password, semver, mac_address
Real World Examples
Extract Date Parts
from zeroreg import digit, capture
date_pattern = (
capture(digit(4), 'year')
.then('-')
.then(capture(digit(2), 'month'))
.then('-')
.then(capture(digit(2), 'day'))
)
match = date_pattern.match('2024-03-15')
print(match.group('year')) # '2024'
print(match.group('month')) # '03'
print(match.group('day')) # '15'
Password Validation
from zeroreg import start_of_line, end_of_line, any_char, digit, letter, lookahead, char_in
password = (
start_of_line()
.then(lookahead(any_char().zero_or_more().then(digit())))
.then(lookahead(any_char().zero_or_more().then(letter())))
.then(lookahead(any_char().zero_or_more().then(char_in('@$!%*?&'))))
.then(any_char().between(8, 32))
.then(end_of_line())
)
password.test('MyP@ssw0rd') # True
password.test('weak') # False
Also Available
- JavaScript/TypeScript:
npm install zeroreg
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zeroreg-0.1.0.tar.gz.
File metadata
- Download URL: zeroreg-0.1.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b267a56557f80a4397ab1eae7de1023bcb1f2b22c27948c27625b783a51a201
|
|
| MD5 |
844500a807304a215925d42eaa63d15a
|
|
| BLAKE2b-256 |
e40d29a3a4f5b50f71a7a36c9171b9afaef6e9b9f15a1106ac720cd1e1b0d7d0
|
File details
Details for the file zeroreg-0.1.0-py3-none-any.whl.
File metadata
- Download URL: zeroreg-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2a02ab2028de0bb722a34d022c549dc829411816723dd89da2180e03e518a9c
|
|
| MD5 |
aa093f9be7a3005f4b5939fd8e23334a
|
|
| BLAKE2b-256 |
a0d77721c04104ee8f190d7a185ff5522ffd53d9a18e5850104ba6ff7ebc7a13
|