Exercises
RegEx
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Find every run of digits in the text.
Python
import re
text = "call 0176 1234567"
# print the list of digit runs
\\d+ matches one or more digits. Use a raw string.
import re
text = "call 0176 1234567"
print(re.findall(r"\d+", text))Exercise 2Passed
search returns None here. Print 'nothing matched' rather than crashing.
Python
import re
found = re.search(r"\d+", "no digits here")
print(found.group())Check the result before calling .group().
import re
found = re.search(r"\d+", "no digits here")
if found:
print(found.group())
else:
print("nothing matched")Exercise 3Passed
The greedy pattern swallows the whole string. Make it match each tag separately.
Python
import re
html = "<b>bold</b>"
print(re.findall(r"<.+>", html))A ? after the + makes it lazy.
import re
html = "<b>bold</b>"
print(re.findall(r"<.+?>", html))