Skip to main content

zeroReg

Regex was a mistake. Just like your ex.

Write regex without the tears or confusion.
A human-readable regex builder for Python.

PyPI version PyPI downloads license python versions


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

Metadata

Release files for zeroreg 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zeroreg 0.1.0
File Size Uploaded Provenance Actions
zeroreg-0.1.0.tar.gz 11.8 kB Without provenance Details for zeroreg-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for zeroreg 0.1.0
File Interpreter ABI Platform Actions
zeroreg-0.1.0-py3-none-any.whl Python 3 none any Details for zeroreg-0.1.0-py3-none-any.whl

Total release size: 23.9 kB

Release files / zeroreg-0.1.0.tar.gz

Download URL zeroreg-0.1.0.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6b267a56557f80a4397ab1eae7de1023bcb1f2b22c27948c27625b783a51a201
BLAKE2b-256 checksum
How to use checksums
e40d29a3a4f5b50f71a7a36c9171b9afaef6e9b9f15a1106ac720cd1e1b0d7d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / zeroreg-0.1.0-py3-none-any.whl

Download URL zeroreg-0.1.0-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c2a02ab2028de0bb722a34d022c549dc829411816723dd89da2180e03e518a9c
BLAKE2b-256 checksum
How to use checksums
a0d77721c04104ee8f190d7a185ff5522ffd53d9a18e5850104ba6ff7ebc7a13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for zeroreg 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page