Skip to main content

pattex

A Python library for extracting common patterns from text using regex — emails, URLs, phone numbers, IP addresses, and more.

Installation

pip install pattex

Changelog

[0.4.0] - 2026-05-16

Added

url_extractor.py

  • extract_urls_by_scheme(text, scheme) — filter URLs by a specific scheme
  • extract_urls_by_domain(text, domain, include_subdomains) — filter URLs by domain, with optional subdomain matching
  • extract_urls_by_tld(text, tld) — filter URLs by TLD, leading dot optional
  • extract_secure_urls(text) — returns only https, ftps, sftp, wss URLs
  • extract_insecure_urls(text) — returns only http, ftp, ws URLs
  • extract_urls_with_auth(text) — returns only URLs containing userinfo (user:pass@host)
  • extract_urls_with_port(text, port) — returns URLs with any port, or a specific port
  • extract_unique_domains(text, root_only) — unique hostnames or root domains across all URLs
  • is_url(text, strict) — validates whether a string is a valid URL
  • allow_ipv4, allow_ipv6, allow_port, allow_tail, schemes parameters added to extract_urls
  • _build_strict_regex() — dynamic regex builder with lru_cache(maxsize=16)
  • _build_permissive_regex() — single combined permissive regex with named groups, lru_cache(maxsize=1)

ip_extractor.py ← new file

  • extract_ip_urls(text, ipv4, ipv6) — extract URLs whose host is an IP address
  • extract_localhost_urls(text, allow_port, allow_tail) — extract localhost URLs only

regexes.py

  • _SCHEMES — raw scheme alternation string
  • _BARE_DOMAIN — raw bare domain pattern without port/tail

_utils.py

  • _get_root_domain(host) — extracts root domain from hostname
  • has_auth(url) — checks whether a URL contains userinfo
  • _is_ip_host(url) — checks whether a URL host is an IP address

extractor_constants.py

  • URL_SCHEMES — Literal type for all supported schemes

Changed

  • include_localhost renamed to allow_localhost — breaking change
  • Permissive mode now runs a single finditer scan instead of 3–4 separate loops — protocol-relative, bare domain, bare IP, and localhost combined into one regex via named groups ((?P<name>...))
  • match.lastgroup used to dispatch per-branch guard logic in permissive loop
  • Localhost always included in permissive mode implicitly — allow_localhost only controls strict mode
  • _build_permissive_regex takes no arguments — localhost always included in permissive
  • Schemed http://localhost always extracted in strict mode regardless of allow_localhost
  • extract_secure_urls and extract_insecure_urls have no mode parameter — scheme implies strictness

Internal

  • All raw pattern constants kept as plain strings in regexes.py — compiled only at assembly time
  • lru_cache on both builders avoids recompilation on repeated calls with same flags
  • urlparse import consolidated at top of url_extractor.py
  • Walrus operator used in extract_urls_with_port to avoid double urlparse call

v0.3.1 - 2026-05-13

Fixed

  • Zoho extractor: removed hyphen (-) and plus (+) from allowed local part characters — Zoho usernames only permit letters, numbers, dots, and underscores per official documentation
  • Zoho extractor: + is a subaddressing tag separator, not a valid account creation character; removed from base regex and validation logic
  • Proton extractor: tightened leading/trailing character rule — local part must begin and end with an alphanumeric character (was checking against ._- set, now explicitly uses isalnum() to match Proton's documented rule)

Changed

  • Renamed email_extraction.py → email_extractor.py
  • Renamed urls_extraction.py → url_extractor.py
  • Updated docstrings for extract_zoho_emails and extract_proton_emails to reflect corrected provider rules
  • Added inline comments on each validation rule inside email_extractor.py for all six provider extractors
  • Added documentation comments for all email regex patterns in constants/regexes.py and corresponding .pyi stubs

Internal

  • General code cleanups across extractor modules

Status

Currently under active development. Full documentation and usage examples coming soon.

License

MIT

Metadata

Release files for pattex 0.4.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 pattex 0.4.0
File Size Uploaded Provenance Actions
pattex-0.4.0.tar.gz 19.2 kB Without provenance Details for pattex-0.4.0.tar.gz

Built distribution (wheel)

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

Total release size: 43.1 kB

Release files / pattex-0.4.0.tar.gz

Download URL pattex-0.4.0.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ac4daee99a67494bdb6dc75f3fd9e167adb3c95bda54ed113acdc9e9d67e55a4
BLAKE2b-256 checksum
How to use checksums
1bb59cb61b612f38782b869d17da8e0492bf81d741c1630e7c388f2d9824a149
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.0

Release files / pattex-0.4.0-py3-none-any.whl

Download URL pattex-0.4.0-py3-none-any.whl
Size 23.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca40c139b5fdc436aadc791f7df584e9c587d47b60d0196b454c8470babd6c30
BLAKE2b-256 checksum
How to use checksums
0f36d892b8ca0407bec5b46ce45f2006bf56533732884db548476f04de69010e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.0

Security for pattex 0.4.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.

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