Complete the code to extract all email addresses from the text using a regular expression.
import re text = "Contact us at support@example.com or sales@example.org" emails = re.findall([1], text) print(emails)
The regular expression [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} matches typical email addresses in the text.
Complete the code to extract all dates in the format YYYY-MM-DD from the text.
import re text = "The event is on 2024-06-15 and registration ends 2024-05-30." dates = re.findall([1], text) print(dates)
The regex \d{4}-\d{2}-\d{2} matches dates in the format YYYY-MM-DD.
Fix the error in the code to extract phone numbers in the format (XXX) XXX-XXXX.
import re text = "Call me at (123) 456-7890 or (987) 654-3210." phones = re.findall([1], text) print(phones)
The regex \(\d{3}\) \d{3}-\d{4} correctly matches phone numbers with parentheses and space.
Fill both blanks to create a dictionary of words and their lengths from the text, including only words longer than 4 letters.
text = "Machine learning extracts useful data from text" words = text.split() lengths = { [1] : [2] for word in words if len(word) > 4 } print(lengths)
The dictionary comprehension uses each word as key and its length len(word) as value, filtering words longer than 4 letters.
Fill all three blanks to create a dictionary of uppercase words and their lengths, including only words with length greater than 3.
text = "Data extraction from text is useful" words = text.split() result = { [1] : [2] for word in words if [3] } print(result)
The dictionary comprehension uses the uppercase version of each word as key, its length as value, and filters words longer than 3 letters.