Skip to main content

Detecting personal data

Every category is detected without configuration. Formats with check digits — cards, IBANs, VINs, many national and tax IDs — are confirmed by them; free-form data such as names and addresses is found by patterns and the words around them. Categories lists which category is validated, and which presets detect it.

Categories​

Personal information​

CategoryExamplesHow it is recognised
emailalice@example.comRFC 5321 pattern
phone+1 (555) 867-5309, (11) 91234-5678, 06 12 34 56 78North American, international, and national formats of Brazil, France, the UK, Spain, Italy and the German-speaking countries
ssn123-45-6789Pattern and allocation rules
nameDear Alice Smith, Patient: John DoeGreetings, titles and labels before the name
date-of-birth1990-04-15, April 15, 1990Several date formats
address123 Main St, Springfield, IL 62701, Musterstraße 12, 10115 Berlin, Av. Paulista, 1578Patterns and street keywords (US, UK, European and Brazilian)
passportA12345678, P1234567Country-specific patterns
drivers-licenseD123-4567-8901, F123-456-78-910-1State and country patterns
national-idAadhaar, NINO, CPF, RG, DNI, codice fiscale, NIR, BSN and moreCountry-specific patterns; check digits in the span engine

Financial​

CategoryExamplesHow it is recognised
credit-card4111 1111 1111 1111Luhn check digit
ibanGB82 WEST 1234 5698 7654 32MOD-97 (ISO 13616)
bank-accountRouting and account numbers, SWIFT/BIC, PIX keys and payloadsKeywords; ABA check digits; PIX CRC-16
cryptocurrencyBTC, ETH and XRP wallet addressesAddress format of each chain
tax-idEIN 12-3456789, VAT GB123456789, CNPJ 11.222.333/0001-81Country patterns; CNPJ check digits

Healthcare​

CategoryExamplesHow it is recognised
medical-recordMRN: 1234567, MR#00456789Keywords and patterns
health-insuranceSubscriber ID, Group/Member #Patterns
prescriptionRx# 1234567, DEA numbersPatterns; DEA check digit

Digital identity​

CategoryExamplesHow it is recognised
ipv4192.168.1.1, 10.0.0.0/8Octet ranges
ipv62001:0db8::8a2e:0370:7334Full and compressed forms
urlhttps://example.com/path?q=1http and https URLs
api-keyBearer tokens, AWS keys, GitHub tokens, …Each provider's key format
social-media@username, profile URLs, handlesPatterns

Vehicles and transport​

CategoryExamplesHow it is recognised
vin1HGCM82633A004352Check digit (position 9)
license-plateABC-1234, AB12CDECountry patterns
tracking-numberFedEx, UPS, USPS and DHL tracking codesEach carrier's format
CategoryExamplesHow it is recognised
case-number2023-CV-001234, CR-2022-5678Patterns
company-registrationEIN, CRN, SIREN, ABN and moreCountry patterns
Names in free text

Patterns find names after a greeting, a title or a label. To find every name in prose, run a named entity recognition model first and pass its findings in as a custom detector.

Narrow detection​

import { anonymize } from "anonyma";

// Only some categories
anonymize("alice@example.com — call 555-867-5309", { enabledCategories: { email: true } }).text;
// "[REDACTED] — call 555-867-5309"

// Values that are known to be safe, as strings or regular expressions
anonymize("noreply@example.org or alice@example.com", {
allowlist: ["noreply@example.org"],
}).text;
// "noreply@example.org or [REDACTED]"

// Only matches the detectors are confident about
anonymize(text, { confidenceThreshold: 0.8 });

detect(text, categories) and hasPII(text, categories) take the categories as a list.

Widen detection​

Aggressive mode uses more permissive patterns, which catch obfuscated values — and more false positives:

anonymize("user [at] example [dot] com", { aggressive: true }).text;
// "[REDACTED]"

Add your own formats​

A custom pattern catches a format of your own, with its own label:

anonymize("Order ACME-001234 confirmed.", {
customPatterns: [{ pattern: /\bACME-\d{6}\b/g, label: "[ORDER_ID]" }],
}).text;
// "Order [ORDER_ID] confirmed."

In the span engine, a detector can have a category of its own with its own strategy, and a pattern can require check digits or keywords nearby — see Span engine:

import {
constant,
createPipeline,
defineRegexDetector,
emailDetector,
redactWith,
} from "anonyma/engine";

const employeeId = defineRegexDetector({
category: "employee-id",
pattern: /\bEMP-\d{6}\b/g,
confidence: 0.95,
requires: ["EMP-"], // skip texts that cannot contain a match
});
const pipeline = createPipeline({
detectors: [emailDetector, employeeId],
replace: { fallback: redactWith(), byCategory: { "employee-id": constant("[EMPLOYEE]") } },
});
pipeline.transform("EMP-001234 wrote from alice@example.com").text;
// "[EMPLOYEE] wrote from [REDACTED]"

Replace a built-in detector​

customDetectors replaces the detector of a category with a function that returns matches:

anonymize("Mail ana@example.com, ops@example.com", {
customDetectors: {
// Only the shared mailbox counts as an email address here
email: (text) =>
[...text.matchAll(/\bops@example\.com\b/g)].map((m) => ({
category: "email" as const,
value: m[0],
start: m.index,
end: m.index + m[0].length,
confidence: 1,
})),
},
}).text;
// "Mail ana@example.com, [REDACTED]"

To ship detectors, strategies and validators together, write a plugin.