Skip to main content

A fluent, chainable API for building regular expressions that read like English

Project description

readable-regex

A fluent, chainable Python API for building regular expressions that read like English.

Documentation

Install

pip install readable-regex

Quick Start

from readable_regex import regex

# Email pattern
regex.words.then('@').words.then('.').words.test("user@example.com")  # True

# Phone number
regex.digit.exactly(3).then('-').digit.exactly(3).then('-').digit.exactly(4).test("123-456-7890")  # True

# Extract all numbers
regex.digits.find_all("Order #42 has 3 items totaling $129")  # ['42', '3', '129']

Vocabulary

The API uses a plural convention: singular = exactly one, plural = one or more.

Items — what you match

Singular Plural (1+) Regex
digit digits \d / \d+
word words \w / \w+
letter letters [a-zA-Z] / [a-zA-Z]+
whitespace whitespaces \s / \s+
any_char any_chars . / .+
then('text') escaped literal
any_of('a', 'b') [ab] or (?:foo|bar)

Modifiers — how you constrain

Modifier Example Effect
exactly(n) digit.exactly(3) \d{3}
between(n, m) digit.between(1, 3) \d{1,3}
optional digit.optional \d?
zero_or_more digit.zero_or_more \d*
starts_with(text?) starts_with('Hello') ^Hello
ends_with(text?) ends_with('!') !$
ignore_case case-insensitive flag
multiline multiline flag
exclude.digits \D+ (negated class)
excluding('_') words.excluding('_') [^\W_]+
capture(builder) capture(regex.words) (\w+)

Execution — terminal methods

Method Returns
test(text) bool
search(text) re.Match | None
match(text) re.Match | None
find_all(text) list[str]
replace(text, repl) str
split(text) list[str]
compile() re.Pattern (cached)
.pattern raw regex string

Examples

Email validation

email = regex.words.then('@').words.then('.').words
email.test("user@example.com")   # True
email.test("bad@@address")       # False
email.pattern                    # '\w+@\w+\.\w+'

Phone number

phone = (
    regex
    .digit.exactly(3).then('-')
    .digit.exactly(3).then('-')
    .digit.exactly(4)
)
phone.test("123-456-7890")  # True
phone.pattern               # '\d{3}\-\d{3}\-\d{4}'

IP address

ip = (
    regex
    .digit.between(1, 3).then('.')
    .digit.between(1, 3).then('.')
    .digit.between(1, 3).then('.')
    .digit.between(1, 3)
)
ip.test("192.168.1.1")  # True

Capturing groups

kv = regex.capture(regex.words).then('=').capture(regex.any_chars)
m = kv.search("color=blue")
m.group(1)  # 'color'
m.group(2)  # 'blue'

Case-insensitive matching

greeting = regex.starts_with('hello').ignore_case
greeting.test("HELLO world")  # True
greeting.test("hey there")    # False

Search and replace

regex.digits.replace("My SSN is 123-45-6789", "***")
# 'My SSN is ***-***-***'

Splitting text

regex.then(',').whitespace.zero_or_more.split("a, b,c, d")
# ['a', 'b', 'c', 'd']

Negated classes

regex.exclude.digits.find_all("a1b2c3")  # ['a', 'b', 'c']
regex.words.excluding('_').pattern        # '[^\W_]+'

Immutable builder — safe reuse

base = regex.starts_with('LOG-')
errors = base.then('ERROR').any_chars
warns  = base.then('WARN').any_chars

errors.test("LOG-ERROR disk full")  # True
warns.test("LOG-WARN low memory")   # True
base.pattern                        # '^LOG\-' (unchanged)

Requirements

  • Python 3.10+
  • Zero runtime dependencies

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

readable_regex-0.2.0.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

readable_regex-0.2.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file readable_regex-0.2.0.tar.gz.

File metadata

  • Download URL: readable_regex-0.2.0.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for readable_regex-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1ceb997c3a3fffa7bbe6ff91b13cf7a99e26e1fbf153456672c14c1a524ee3ac
MD5 a95702c8048ed4529df6828e30c24229
BLAKE2b-256 d809c34093255fc9c1365631c527223407ed20ec55ea510a835e6d3838393b8d

See more details on using hashes here.

File details

Details for the file readable_regex-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: readable_regex-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for readable_regex-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c6d635289ecfddf8efb5d050f595605beca47b6680df7af193a1d7a7f7815b0d
MD5 58b142f4955a0ead76b1f68fb70d8674
BLAKE2b-256 5d42c30aceaa078267e350549e6a1728caeb8c442ee06a1ee7f581acf1036e8a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page