Skip to main content

Write regex using plain English keywords — no more memorizing syntax.

Project description

humanregex 🧠

Write regex using plain English — no more memorizing cryptic syntax.

PyPI version Python License: MIT

Quick start

python
from humanregex import Pattern

# Match a 7-digit phone number
Pattern().digit(3).dash().digit(4).match("123-4567")   # True

# Find all numbers in a string
Pattern().digit().one_or_more().find_all("Call 123 or 4567")  # ['123', '4567']

# Replace numbers
Pattern().digit().one_or_more().replace("Ref 2024-42", "NUM")  # 'Ref NUM-NUM'

# Build a pattern for a name with honorific
(
    Pattern()
    .starts_with()
    .one_of("Mr", "Ms", "Dr")
    .space()
    .letters(min_count=2)
    .ends_with()
    .match("Dr Smith")   # True
)

Installation

bash
pip install human-regex-lib

Or install from source for local development:

bash
git clone https://github.com/YOUR_USERNAME/humanregex
cd humanregex
pip install -e ".[dev]"

API — v0.1.1

Anchors

Method Regex Description
.starts_with() ^ Anchor to start of string
.ends_with() $ Anchor to end of string

Character Classes

Method Regex Description
.digit(n=1) \d{n} Exactly n digits
.digits(min, max) \d{min,max} Range of digits
.letter(n=1) [a-zA-Z]{n} Exactly n letters
.letters(min, max) [a-zA-Z]{min,max} Range of letters
.alphanumeric(n=1) \w{n} Letters, digits, underscore
.whitespace(n=1) \s{n} Whitespace characters
.any_char(n=1) .{n} Any character (not newline)

Literals

Method Matches
.literal("text") Exact string (auto-escaped)
.dash() -
.dot() .
.space(n=1) One or more spaces
.underscore() _
.at() @

Quantifiers

Method Regex Description
.one_or_more() + One or more of previous
.zero_or_more() * Zero or more of previous
.optional() ? Previous token is optional

Grouping & Alternation

Method Description
.one_of("a", "b", "c") Match one of the given strings
.one_of_chars("aeiou") Match one character from the set
.not_chars("0-9") Match any character NOT in the set
.group(pattern) Add a capturing group from raw regex text
.named_group(name, pattern) Add a named capturing group from raw regex text

Pre-built Patterns

Method Description
.word_boundary() Add a word boundary (\b)
.email() Match a practical email pattern
.url(require_scheme=True) Match an http/https URL
.ip_address() Match an IPv4 address

Flags

Method Description
.ignore_case() Case-insensitive matching
.multiline() ^/$ match each line boundary

Actions

Method Returns Description
.match(text) bool Full string must match
.search(text) `Match None`
.find_all(text) list All non-overlapping matches
.find_iter(text) iterator Iterate over match objects
.count(text) int Count non-overlapping matches
.replace(text, repl) str Replace all matches
.split(text) list Split string by pattern
.compile() re.Pattern Compiled regex for reuse

Introspection

Method Description
.build() Return the raw regex string
.explain() Print pattern + flags for debugging

Running tests

bash
pip install -e ".[dev]"
pytest

Roadmap

  • v0.2.0 — Pre-built patterns (phone_number(), credit_card(), …)
  • v0.3.0 — Advanced grouping and capture helpers
  • v0.4.0 — Lookaheads and lookbehinds
  • v1.0.0 — Stable public API

Pull requests are welcome! Please open an issue first to discuss what you'd like to change.

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

human_regex_lib-0.1.2.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

human_regex_lib-0.1.2-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file human_regex_lib-0.1.2.tar.gz.

File metadata

  • Download URL: human_regex_lib-0.1.2.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for human_regex_lib-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ed5fdaccbfe807863375e4fe47891e5299ebbaa68c8de2823b10f2df8b08c76e
MD5 b81f243850c11431c4fec18a21e8e95f
BLAKE2b-256 2bbbdfbe8aa44b33a2be34be857908a3a9b28e66e5021035fe1db7476f85b26f

See more details on using hashes here.

File details

Details for the file human_regex_lib-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for human_regex_lib-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b21e19c1c8328e45f5f692d0cd86bd7ddac11dc62e893133aa50e6c8c726a8f9
MD5 e708aa901432d6b94db02717f6a11964
BLAKE2b-256 ae79511c983cc9ca764d1c371b1b4df989d603c8f102a7b341502d7a891c3b32

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