BasicsSecurityGuardrailsOpenAIAnthropicGoogle
Problem
Implement redact_pii(text) that masks all email addresses with "[EMAIL]" and all US Social Security Number patterns (XXX-XX-XXXX) with "[SSN]" while preserving the rest of the string.
Why It Matters: Guardrail firewalls scrub sensitive PII before sending customer messages to third-party LLM providers or writing agent traces to observability logs.
Function Signature
import re
def redact_pii(text: str) -> str:
"""Replace emails with '[EMAIL]' and SSNs (XXX-XX-XXXX) with '[SSN]'."""
Sample Input & Output
Example 1: Mixed Email & SSN
Input:
text = "Contact alice.smith@acme.com or SSN 123-45-6789."
Expected Output:
"Contact [EMAIL] or SSN [SSN]."
Explanation: Both the email address and the 9-digit hyphenated SSN are masked cleanly.
Example 2: Multiple Matches & Clean Text
Input:
text = "No PII here, order #98765."
Expected Output:
"No PII here, order #98765."
Explanation: Non-PII numbers and normal text pass through untouched.
Constraints & Edge Cases
- Return the redacted string.
- Match word boundaries around SSNs (
\b\d{3}-\d{2}-\d{4}\b) so longer hyphenated IDs aren't partially mangled. - Handle multiple emails/SSNs in the same string.
Constraints & Evaluation Criteria
- Return the redacted string.
Hints
Learn the Concept First
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
alice@acme.com, 123-45-6789
Expected Output / Criteria
'Email [EMAIL], SSN [SSN]'