Regular Expression in Python
Level 5234 keystrokes
This uses regex to find and replace email addresses. re module provides pattern matching with findall() and substitution with sub().
Python code
# Import the re (regular expression) module1import re2 # Text containing email addresses3text = "Email: test@example.com, support@site.org"# Regex pattern for emails:# [\w.]+ matches word chars and dots (username)# @ matches the @ symbol# [\w.]+ matches word chars and dots (domain)4pattern = r"[\w.]+@[\w.]+"5 # findall returns all non-overlapping matches as a list6matches = re.findall(pattern, text)7print(f"Found emails: {matches}")8 # sub replaces all matches with the replacement string9result = re.sub(pattern, "[REDACTED]", text)10print(f"Redacted: {result}")Output
Found emails: ['test@example.com', 'support@site.org'] Redacted: Email: [REDACTED], [REDACTED]
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Python code and how often they appear.
"× 10.× 6,× 4(× 4)× 4=× 4:× 3[× 3]× 3@× 3