Skip to main content

Command Palette

Search for a command to run...

My Regex Learning Journey (TryHackMe + RegexOne)

Published
4 min read

Lately, I’ve been diving into regular expressions (regex). At first, it felt like staring at some alien code — but as I kept practicing, I started to see the patterns. It’s been frustrating at times, but also really rewarding. Here’s a walkthrough of what I’ve studied so far, starting with TryHackMe and continuing with RegexOne.

Part 1: Getting Started on TryHackMe

On TryHackMe, I worked on an exercise where the task was to match emails and also capture the username and domain name separately (without the .com).

✅ My regex looked like this:

(\w+)@(\w+)\.com
  • (\w+) before the @ captures the username.

  • (\w+) after the @ captures the domain.

  • The \.com makes sure the email ends with .com.

That got me through the challenge and gave me a push to keep going.

Part 2: RegexOne Lessons

After TryHackMe, I moved on to RegexOne for more structured practice. Here’s the breakdown:

Lesson 1: Matching Characters

Pattern:

abc

This simply matched the common substring across all given lines.

Lesson 1½: Matching Digits

Pattern:

\d+

The \d matches digits, and + makes sure we grab one or more of them.

Lesson 2: The Dot (Wildcard)

Pattern:

...

Three dots match any three characters, which worked for cat., 896., and ?=+., but not abc1.

Lesson 3: Matching Specific Characters

Pattern:

[cmf]an

Here [cmf] means match c, m, or f, giving us can, man, fan.

Lesson 4: Excluding Specific Characters

Pattern:

[^b]og

This excludes b, so it matched hog, dog, but skipped bog.

Lesson 5: Character Ranges

Pattern:

[A-C]..

This matches three-letter words starting with A, B, or C.

Lesson 7: Repeated Characters (Kleene Star & Plus)

Pattern:

a+b*c+
  • a+ → one or more a

  • b* → zero or more b

  • c+ → one or more c

Lesson 8: Optional Characters

Pattern:

files?

The ? makes s optional, so both file and files matched.

Lesson 9: Whitespace

Pattern:

\d\.\s+abc

Here \s+ grabs one or more spaces/tabs, which handled the indentation before abc.

Lesson 10: Start and End Anchors

Pattern:

^Mission: successful$
  • ^ anchors the start.

  • $ anchors the end.

Lesson 11: Match Groups

Pattern:

^(.+)\.pdf$

The group (.+) captures the filename before .pdf.

Lesson 12: Nested Groups

Pattern:

^(\w+)\s+(\d+)$
  • First group captures the month.

  • Second group captures the year.

Lesson 13: More Group Work

Pattern:

(\d+)x(\d+)

This cleanly captured width × height in resolutions.

Lesson 14: Conditional OR

Pattern:

I love (cats|dogs)

Used | for OR to allow both cats and dogs.

Lesson 15: Other Special Characters

Pattern:

\w+

This matched whole words like fox, students, etc.

Part 3: RegexOne Problems

After lessons, I tackled the Problems section. These were more like real-world cases.

Problem 1: Matching Numbers

Pattern:

-?\d+(\.\d+)?
  • -? handles optional negatives.

  • \d+ = digits.

  • (\.\d+)? handles optional decimals.

Problem 2: Matching Phone Numbers

Pattern:

\(?(\d{3})\)?[-\s]?(\d{3})[-\s]?(\d{4})

This grabbed phone numbers in different formats while capturing the area code.

Problem 3: Matching Emails

Pattern:

([\w.]+)@([\w.]+)

Captured the username before @ and domain after.

Problem 4: Matching HTML

Pattern:

<(\w+)[^>]*>.*?</\1>

Captured the tag name (like a, div) while ensuring open/close matched.

Problem 5: Matching Filenames

Pattern:

(\w+)\.(jpg|png|gif)

Captured only image filenames and their extensions.

Problem 6: Trimming Whitespace

Pattern:

^\s*(.*?)\s*$

Stripped spaces at start and end, captured the clean line.

Problem 7: Extracting from Logs

Pattern:

\.(\w+)\((\w+\.java):(\d+)\)

Captured method, file, and line number from stack traces.

Problem 8: Extracting Data from URLs

Pattern:

^(\w+)://([\w.-]+)(?::(\d+))?
  • First group: protocol (ftp, https, etc).

  • Second group: domain/host.

  • Third group (optional): port number.

Example:

Reflections

When I first saw regex, it looked impossible. But step by step, lesson by lesson, it started to make sense. I learned how tiny symbols — ., [], +, ?, ^, $ — can become powerful tools for searching and extracting data.

It’s still tricky sometimes (and yeah, I rage a bit when it doesn’t click), but this journey has made me way more confident in tackling real-world text parsing.