Skip to main content

Regex-based data extraction with typed normalization

Project description

renorm

PyPI version Python versions License CI

Regex-based data extraction with typed normalization.

renorm lets you embed small, reusable “spec” objects inside regular expressions. Each spec defines both a regex fragment and a normalization function, so matched values are returned as real Python types with zero post-processing.

Normalization depends on the exact structure of the matched text, so extraction and normalization naturally belong together. renorm follows this principle by treating each spec as a single unit of pattern + normalization logic, making typed extraction composable and predictable.

Highlights

  • Drop-in wrapper around Python’s re — mirrors the API and adds no dependencies.
  • Built-in spec objects for common data types.
  • Define your own specs with custom patterns and normalization logic.

Basic Example

Instances of renorm.Num specify the thousand and decimal separators of plain number literals to be captured and, therefore, how they should be normalized.

import renorm as rn

eu = rn.Num(dec=",", ths=" ")
us = rn.Num(dec=".", ths="'")

pat = rn.compile(
    r"price=({@eu}); qty=({@us}); total=({@eu})",
    eu=eu,
    us=us,
)

m = pat.search("price=1 234,50; qty=2'000.0; total=2 469,00")
print(m.groups())  # (1234.5, 2000.0, 2469.0)

Installation

pip install re-norm

Features

  • Extract numeric literals with built-in specs
  • Normalize custom data types via user-defined specs
  • Compose patterns using simple placeholders ({@name})
  • Zero dependencies: drop-in wrapper around Python’s re
  • Minimal developer overhead: mirrors Python’s re API and semantics

Using Specs in Regular Expressions

Specs are embedded in patterns using placeholders:

  • {@name} for keyword specs
  • {@0}, {@1}, … for positional specs

Each placeholder is replaced by the regex fragment defined by the spec. If the placeholder appears inside a capturing group, the matched text is passed through the spec’s normalization function.

This is the same placeholder syntax used in the basic example above, where specs are passed as kwargs.

Custom Specs

You can define your own normalization rules by subclassing rn.NormSpec. A custom spec must implement a pattern property (regex fragment) and a normalize method. This allows renorm to support arbitrary data types and formats.

class Hex(rn.NormSpec):
    @property
    def pattern(self):
        return r"[0-9A-Fa-f]+"

    def normalize(self, value: str):
        return int(value, 16)
addr = Hex()
size = rn.Num()

pat = rn.compile(
    r"addr=0x({@addr}), size=({@size}) bytes",
    addr=addr,
    size=size,
)

m = pat.search("addr=0x1A2B, size=64 bytes")
print(m.groups())   # (6699, 64.0)

API Overview

Mirrors the familiar Python re API.

renorm.compile(pattern, *specs, **named_specs, flags) -> Pattern

renorm.Pattern

  .search(text) -> Match | None

  .match(text) -> Match | None

  .fullmatch(text) -> Match | None

  .pattern -> str   (the compiled regex string)

renorm.Match

  .group(0) -> str   (full match)

  .group(i) -> T   (normalized value)

  .group(i, j) -> tuple[T, str, None, ...]   (normalized values)

  .groups() -> tuple[T, str, None, ...]   (all normalized values)

  .groupdict()   (disabled: reserved for internal use)

renorm.NormSpec

renorm.Num(dec, ths)

Note

Some methods from the re API are not implemented yet. If you need one, just open an issue — happy to add it.

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

re_norm-0.1.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

re_norm-0.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file re_norm-0.1.0.tar.gz.

File metadata

  • Download URL: re_norm-0.1.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"20.04","id":"focal","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for re_norm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2c122c350e4986bb8e31d49ddf792d85cbd21a775c2ffb303f454c96790641f3
MD5 102e504060c85848dc55553eca188c4e
BLAKE2b-256 d2406573ff725b129331d0a3184c33af821aaf542dd171f20749c88b0ee283fb

See more details on using hashes here.

File details

Details for the file re_norm-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: re_norm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"20.04","id":"focal","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for re_norm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a61eeadfd9ad9b74db2c4a0f2d8098883da91ba079bae7db4fcfa183d5af7e65
MD5 0d2dd959f101e1fe52795e06e3316f33
BLAKE2b-256 918a065c7aba8138e75b847788ae904e9aaaa95c59eacdb93cb39bb95c25f46c

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