RedaktAIDocs

RegexDetector

Regex-based entity detection with built-in and custom patterns

Detects PII using regular expressions. Ships with 8 built-in patterns and supports custom patterns.

Import

from rdakt_ai import RegexDetector

Constructor

RegexDetector(
    *,
    custom_patterns: dict[str, str] | None = None,
    detector_priority: int = 0,
)
ParameterTypeDefaultDescription
custom_patternsdict[str, str] | NoneNoneAdditional regex patterns as {entity_type: regex}
detector_priorityint0Priority for overlap resolution (higher wins)

Built-in Patterns

Entity TypePatternExample Match
EMAIL[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}john@example.com
SSN\b\d{3}-\d{2}-\d{4}\b123-45-6789
CREDIT_CARD\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b4111 1111 1111 1111
PHONE\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}(555) 123-4567
IP_ADDRESS\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b192.168.1.1
IBAN\b[A-Z]{2}\d{2}[A-Z0-9]{4,30}\bDE89370400440532013000
JWTeyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+eyJhbG...
API_KEYVarious provider patternssk-proj-..., AKIA..., ghp_..., glpat-...

Methods

detect

def detect(self, text: str) -> list[Entity]

Scan text and return all detected entities.

Class Attributes

AttributeTypeDescription
builtin_patternsdict[str, str]The built-in regex patterns dictionary

Usage

Default (built-in patterns only)

detector = RegexDetector()
entities = detector.detect("Email john@example.com, SSN 123-45-6789")

for entity in entities:
    print(f"{entity.type}: {entity.value} [{entity.start}:{entity.end}]")
# EMAIL: john@example.com [6:22]
# SSN: 123-45-6789 [28:39]

With custom patterns

detector = RegexDetector(custom_patterns={
    "EMPLOYEE_ID": r"EMP-\d{6}",
    "ACCOUNT": r"\d{4}-\d{4}-\d{4}",
})

entities = detector.detect("Contact EMP-123456 about account 1234-5678-9012")
# Detects both EMPLOYEE_ID and ACCOUNT, plus any built-in patterns

See Also

On this page