

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Split Delimiter
incomplete
2: Regex
incomplete
3: Extract Links
incomplete
4: Split Images and Links
incomplete
5: Text to TextNodes
incomplete
This lesson's interactive features are locked, please to keep using them
Time to extract the links and images from our Markdown using regex.
The findall function that will return a list of all the matches in a string.
import re
text = "I'm a little teapot, short and stout. Here is my handle, here is my spout."
matches = re.findall(r"teapot", text)
print(matches) # ['teapot']
text = "My email is [email protected] and my friend's email is [email protected]"
matches = re.findall(r"(\w+)@(\w+\.\w+)", text)
print(matches) # [('lane', 'example.com'), ('hunter', 'example.com')]
Use regexr.com for interactive regex testing, it breaks down each part of the pattern and explains what it does.
There are spoilers in the tip section if you don't want to figure out the regex patterns yourself.
text = "This is text with a  and "
print(extract_markdown_images(text))
# [("rick roll", "https://i.imgur.com/aKaOqIh.gif"), ("obi wan", "https://i.imgur.com/fJRm4Vk.jpeg")]
text = "This is text with a link [to boot dev](https://www.boot.dev) and [to youtube](https://www.youtube.com/@bootdotdev)"
print(extract_markdown_links(text))
# [("to boot dev", "https://www.boot.dev"), ("to youtube", "https://www.youtube.com/@bootdotdev")]
def test_extract_markdown_images(self):
matches = extract_markdown_images(
"This is text with an "
)
self.assertListEqual([("image", "https://i.imgur.com/zjjcJKZ.png")], matches)
Run and submit the CLI tests from the root of the project.
Below are spoilers!!! You don't need to be a regex master for this course, but if you want to challenge yourself, try to write the regexes without looking below. If you don't care, I've provided them for you.
# images
r"!\[([^\[\]]*)\]\(([^\(\)]*)\)"
# regular links
r"(?<!!)\[([^\[\]]*)\]\(([^\(\)]*)\)"