#354PII Detection & RedactionMedium

PII Detection & Redaction

Background

RAG and agent stacks must redact PII (email addresses, phone numbers, SSNs, credit-card numbers) before storing user prompts or surfacing them to humans. Regex-based redaction is the cheapest first line of defence; ML-based detectors come later.

Problem statement

Implement redact_pii(text) returning the input with these entities replaced:

  • Email addresses → [EMAIL]
  • US phone numbers (NNN-NNN-NNNN with optional spaces or dots) → [PHONE]
  • Social Security Numbers (NNN-NN-NNNN) → [SSN]

Input

  • textstr (or None / empty).

Output

Returns a str with all matches replaced inline.

Examples

Example 1 — email

Input:  "contact test@example.com"
Output: "contact [EMAIL]"

Example 2 — US phone

Input:  "call 555-123-4567 today"
Output: "call [PHONE] today"

Example 3 — SSN

Input:  "ssn 123-45-6789"
Output: "ssn [SSN]"

Example 4 — multiple entities in one string

Input:  "email a@b.com phone 555-123-4567"
Output: "email [EMAIL] phone [PHONE]"

Example 5 — plain text passes through

Input:  "Hello world"
Output: "Hello world"

Constraints

  • None / empty input returns "".
  • Apply SSN before phone (SSN is 3-2-4, phone is 3-3-4; both could partially match the same digits).
  • Returns a plain Python str.

Notes

  • Coverage holes. International phone numbers, names, addresses, dates of birth, IP addresses — all PII, none covered by this kernel. Production stacks pair regex with NER models.
  • Locale. US-only phone format; UK, India, etc. use different formats and need their own patterns.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: redact an email
  • Example: redact a US phone number
  • Sample: redact an SSN