Removing personal data before text is sent to a model is the first privacy guard.
What to build
A program that hides the emails and phone numbers in a line of text.
Requirements
- Replace every email address by
[EMAIL]. - Then replace every phone number by
[PHONE]. Use the patterns in the hints. - Print the changed text. All other text stays exactly as it was.
Input
One line of text.
Output
One line.
Constraints
The line has at most 300 characters.
Example
Input:
Mail ana@example.com now
Output:
Mail [EMAIL] now
Why it works
Regular expressions describe the shape of an email or a phone number, so every match can be replaced no matter which person it belongs to.
Examples
Example 1 · Input
Mail ana@example.com now
Expected output
Mail [EMAIL] now
Example 2 · Input
Call +880 1711 223344 today
Expected output
Call [PHONE] today
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Use `re.sub` with a pattern for an email: `[\w.+-]+@[\w-]+\.[a-z]+`.
Hint 2
A phone number starts with an optional `+` and a digit, then at least seven digits, spaces or dashes, and ends with a digit: `\+?\d[\d -]{7,}\d`.
Hint 3
Replace emails first, then phone numbers.
Solution
Show one correct solution (try it yourself first)
import re
text = input()
text = re.sub(r"[\w.+-]+@[\w-]+\.[a-z]+", "[EMAIL]", text)
text = re.sub(r"\+?\d[\d -]{7,}\d", "[PHONE]", text)
print(text)