Reputation: 13
Is there a regex way of redacting email & telephone info from this text:
Hi John,
Please contact me at [email protected] or 212-555-1212 or 313.555.1313.
Thank you.
Tom
so that it becomes this:
Hi John,
Please contact me at [REDACTED] or [REDACTED] or [REDACTED].
Thank you.
Tom
Upvotes: 1
Views: 2715
Reputation: 162831
Use String.replaceAll()
. You can use this regex for matching email addresses:
[^@\s]+@[^@\s]+\.[a-zA-Z]{2,}
And this one for phone numbers:
\d{3}[-.]\d{3}[-.]\d{4}
A greater variety of phone number formats could be supported, but I only covered the examples in your question.
Here is a little test program to prove the above works:
public class Redacted {
public static void main(String[] args) {
String text = "Hi John,\n"
+ "Please contact me at [email protected] or 212-555-1212 or 313.555.1313.\n"
+ "Thank you.\n"
+ "Tom";
System.out.println("Before:\n" + text + "\n");
// replace email addresses
text = text.replaceAll("[^@\\s]+@[^@\\s]+\\.[a-zA-Z]{2,}", "[REDACTED]");
// replace phone numbers
text = text.replaceAll("\\d{3}[-.]\\d{3}[-.]\\d{4}", "[REDACTED]");
System.out.println("After:\n" + text + "\n");
}
}
The above program outputs:
Before:
Hi John,
Please contact me at [email protected] or 212-555-1212 or 313.555.1313.
Thank you.
Tom
After:
Hi John,
Please contact me at [REDACTED] or [REDACTED] or [REDACTED].
Thank you.
Tom
Upvotes: 3