FastPII Docs
Guides

Pattern Registry

The pattern registry system for regex-based detection.

Pattern Registry

FastPII uses a pattern registry system for managing regex-based detection patterns. Each country pack registers its patterns with a shared registry.

How it works

When you register a country pack, its patterns are automatically loaded into the shared pattern registry:

from fastpii import FastPII, DEFAULT_PRIORITY
from fastpii.countries.cz import CzechPack

engine = FastPII(priority=DEFAULT_PRIORITY)
engine.register(CzechPack())

Internally, engine.register() calls:

  1. pack.register_patterns(pattern_registry) - loads regex patterns
  2. pack.register_detectors(detector_registry) - registers detector instances

PatternDefinition

Each pattern is defined as a PatternDefinition:

from fastpii.patterns.base import PatternDefinition

pattern = PatternDefinition(
    name="rodne_cislo_base",
    pattern=r"\b(\d{6})\s*/\s*(\d{3,4})\b",
    pii_type="rodne_cislo",
    region="cz",
)

Shared pattern registry

The pattern registry is a shared singleton:

from fastpii.patterns.registry import get_shared_registry

registry = get_shared_registry()

Patterns are cached with @lru_cache(maxsize=1) on get_region_loaders() for performance.

Custom pattern registration

You can register custom patterns:

from fastpii.patterns.base import PatternDefinition
from fastpii.patterns.registry import get_shared_registry

registry = get_shared_registry()

pattern = PatternDefinition(
    name="custom_employee_id",
    pattern=r"\bEMP-\d{5}\b",
    pii_type="employee_id",
    region="us",
)

registry.register(pattern)

Pattern loading per region

Each country pack provides a PatternLoader that registers region-specific patterns:

RegionPatterns
czrodné číslo, IČO, DIČ, bank account, postal code, phone, email, name, address, date of birth, vehicle plate, health insurance, credit card, IBAN, identity card
plPESEL, NIP, REGON, postal code, phone, address
deSteuer-ID, USt-IdNr, Handelsregister, postal code, phone, address
frSIREN, SIRET, INSEE/NIR, postal code, phone, address

On this page