Neural Mastery
← Practice
Redact PII Patterns From Textmedium

Redact PII Patterns From Text

medium Agents in Production 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement basic PII redaction, a real output-side safety filter before an agent's response (which may echo retrieved content) is shown to a user or logged.

Task

Implement redact_pii(text). Replace every email-like pattern (\S+@\S+\.\S+) with '[EMAIL]' and every US-phone-like pattern (\d{3}-\d{3}-\d{4}) with '[PHONE]'.

Function Signature

redact_pii(text: str) -> str

Examples

Example 1: redacts an email
Input: text = 'Contact me at alice@example.com please.'
Output: 'Contact me at [EMAIL] please.'
Example 2: redacts a phone number
Input: text = 'Call 555-123-4567 for support.'
Output: 'Call [PHONE] for support.'
Example 3: no pii, unchanged
Input: text = 'This text has no personal info.'
Output: 'This text has no personal info.'

Constraints

  • text is a string
Python3Saved ✓
import re

def redact_pii(text):
# Your implementation here
pass

Case 1: redacts an email
Input: text = 'Contact me at alice@example.com please.'
Expected: 'Contact me at [EMAIL] please.'
Case 2: redacts a phone number
Input: text = 'Call 555-123-4567 for support.'
Expected: 'Call [PHONE] for support.'
Case 3: no pii, unchanged
Input: text = 'This text has no personal info.'
Expected: 'This text has no personal info.'