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 RegexDetectorConstructor
RegexDetector(
*,
custom_patterns: dict[str, str] | None = None,
detector_priority: int = 0,
)| Parameter | Type | Default | Description |
|---|---|---|---|
custom_patterns | dict[str, str] | None | None | Additional regex patterns as {entity_type: regex} |
detector_priority | int | 0 | Priority for overlap resolution (higher wins) |
Built-in Patterns
| Entity Type | Pattern | Example 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}\b | 123-45-6789 |
CREDIT_CARD | \b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b | 4111 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}\b | 192.168.1.1 |
IBAN | \b[A-Z]{2}\d{2}[A-Z0-9]{4,30}\b | DE89370400440532013000 |
JWT | eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+ | eyJhbG... |
API_KEY | Various provider patterns | sk-proj-..., AKIA..., ghp_..., glpat-... |
Methods
detect
def detect(self, text: str) -> list[Entity]Scan text and return all detected entities.
Class Attributes
| Attribute | Type | Description |
|---|---|---|
builtin_patterns | dict[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 patternsSee Also
- Detection Pipeline — pipeline architecture
- Custom Detectors — writing your own detector