Pattern Match
You'll learn regular expressions, which are ugly and old and still the fastest way to find a needle in a haystack of text.
About
Regex looks like line noise, and it's worth a weekend anyway. You'll use it in your editor, in grep, and in every language you ever touch. The trap is trying to parse real structure with it, like HTML or dates with time zones. Use it for finding text and checking the shape of text, and keep each pattern small enough to read aloud.
At a glance
- Per requirement
- 25 XP
- Completion bonus
- 150 XP
- Requirements
- 6
- Takes
- One weekend
- Skill level
- Beginner (done it a few times)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Match every phone number in a page of text, whatever format they're in. Paste in twenty numbers in mixed formats with dashes, dots, parentheses and spaces. Write one pattern that catches them all. Use regex101 or your language's own test tool to see the matches highlighted. Take a screenshot of the matches. Photo
-
2
Pull every email address out of a file with grep and a pattern. Use grep -Eo with a pattern on a file you own, like an export of your own sent mail or a server log. Count what it finds and look for false positives. Blur other people's addresses before you post the screenshot. Take a screenshot of the output. Photo
-
3
Use capture groups to reformat dates from one style to another. Turn 2024-03-15 into 15/03/2024 across a whole file using your language's substitute function. Groups are what make regex useful instead of just clever. Take a screenshot of the file before and after. Photo
-
4
Write a pattern that checks a password rule, then break it with an input it gets wrong. Try eight or more characters, one digit and one capital letter. Then feed it an input with a space, a Unicode letter, or nothing at all. Every validation regex has a hole somewhere. Take a screenshot of the pattern, a pass, and the hole. Photo
-
5
Use a regex search and replace in your editor to change 20 lines at once. Rename a variable with a word boundary so you don't catch partial matches, or wrap every line in quotes. Film a short clip of the replace happening in your editor. Video
-
6
Write a pattern you can't read a week later, then rewrite it with comments or named groups. Most languages support a verbose mode or named groups. The first version is for showing it works, and the second is for the person who maintains it. Take a screenshot of both versions. Photo