Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 2

Testing Regex with Regex101

Recommended resources

Hello! Welcome back to your course on using regular expressions for your mail triage project.

In our last session, we established a solid conceptual foundation, defining a regular expression as a formal language for specifying search patterns. Your background in philosophy is a great asset here, as you're already used to working with formal systems and precise definitions.

Introduction

Today's Goal: This lesson moves from theory to practice. By the end of our session, you will be able to use an interactive tool like Regex101 to test and debug patterns against sample text. We'll introduce you to the "laboratory" where you will build, test, and refine your regular expressions throughout this course.

Why it matters: Writing regex can feel like composing a logical argument; it needs to be precise and tested against various cases to ensure it's sound. An interactive tool gives you instant feedback, translating the abstract symbols into concrete results. It's the most effective way to learn, as it turns a potentially frustrating process into one of exploration and discovery.

Time to complete: This lesson should take you about 30 minutes.



1. The Right Tool for the Job: An IDE for Regex

In our previous lesson, we framed regex as a specialized language. The most common mistake people make when learning is trying to write and debug this new language using standard text editors, which is like trying to write a legal contract in a simple notepad—possible, but you're missing all the specialized tools that make the job easier and more reliable.

It's more effective to think of a tool like Regex101 as an IDE (Integrated Development Environment) specifically designed for the language of regular expressions.

Activity 1: The IDE Analogy (5 minutes)

Please read the following short section from the article "Beginner's guide to regular expressions." It perfectly captures this essential mindset.

Beginner's guide to regular expressions | The Man in the Arena

  • Focus on the section titled "Using regular expressions is using a different programming language." The author, Carl Alexander, makes a compelling case for why using a dedicated tool is the single best way to improve your skills.


2. A Guided Tour of Your Regex Workbench

For this course, our IDE of choice will be Regex101.com. It is a powerful, browser-based tool that provides everything we need.

Let's familiarize ourselves with its main components. The interface can be broken down into three key areas, as shown in the image below.

None This image shows the three main panels of the Regex101 tool, which are our primary focus.

  1. Test String (Box 1): This is where you paste the text you want to search through. For our purposes, this will be your sample emails or snippets from them.
  2. Regular Expression (Box 2): This is where you will write your pattern—the "expression" in "regular expression."
  3. Match Information / Explanation (Box 3 & Right Panel): This area is what makes the tool so powerful. It provides a real-time, plain-English explanation of what your pattern is doing, token by token. It also highlights the parts of your test string that match the pattern.

Activity 2: Watch a Demonstration (2.5 minutes)

Now, let's see these components work together. The following video gives a quick, practical demonstration of the tool in action.

Regex101 - The Ultimate Tool for Regular Expressions - DevProTips #1 - Regex Visualizer and Debugger

  • Watch from the beginning until 02:27.
  • Pay attention to:
    • How the matches in the "Test String" are highlighted instantly as the pattern is typed.
    • How the tool shows which strings are valid or invalid against the pattern.
    • How the "Match Information" on the right shows the specific parts of the text that were captured (we'll cover "groups" later, but it's good to see this now).


3. Understanding the Key Features

You've seen the tool in action; now let's solidify our understanding of its most important features. The interactive feedback and detailed explanations are what transform regex from a cryptic puzzle into a logical system.

Activity 3: Read About the Core Features (5 minutes)

The following article provides an excellent summary of why Regex101 is such an effective learning tool.

Mastering Regular Expressions: A Deep Dive into Regex101

  • Read the section "Why Regex101 is the Ultimate Regex Workbench," focusing on the descriptions of:
    1. The Interactive Match Panel
    2. The Explanation Panel
    3. The Quick Reference tab

The Explanation Panel is your most valuable learning aid. It's like having a tutor looking over your shoulder, explaining the meaning of every symbol you type. If you ever feel unsure about what a part of your pattern does, this panel will be your first stop.



4. Your First Hands-On Experiment

It's time to get your hands dirty. We're going to use a pre-configured Regex101 session to see how it works firsthand.

Activity 4: Experiment in the Lab (15 minutes)

  1. Click this link to open Regex101: https://regex101.com/r/sA0jV1/1

    You'll see a workspace that is already set up:

    • The Regular Expression field contains the simple pattern Ref:.
    • The Test String field contains a few lines from a sample email.
  2. Observe the initial state:

    • Notice that Ref: in the test string is highlighted—this is your match.
    • Look at the Explanation panel on the right. It tells you that Ref: matches the characters Ref: literally.
    • Look at the Match Information panel, which shows one full match.
  3. Now, let's experiment. Try making these changes to the pattern in the "Regular Expression" box and observe the results:

    • Change Ref: to Case. Notice how the match instantly jumps to the Case # line.
    • Try to match the reference number ABC-123. Type ABC-123 into the regex box. It matches perfectly.
    • What about the invoice number? Try matching INV.456. Type INV.456 into the regex box.
      • It matches, but look at the Explanation panel. It tells you that the . is a special character that "matches any character (except for line terminators)." This is a preview of a concept we'll cover in the next lesson!
    • To match a literal period, you must "escape" it with a backslash. Change the pattern to INV\.456. Now, the explanation correctly states that \. matches the character . literally. This is a fundamental concept in regex: special characters must be escaped to be treated as literals.
    • Case sensitivity: Change the pattern back to ref:. Notice that there are no matches. By default, regex is case-sensitive. Now, find the small icon that looks like a flag at the end of the regex input field. Click it and select i - case insensitive. Observe how ref: now matches Ref:. Flags are powerful modifiers that change how the entire pattern behaves.

Feel free to play around for a few more minutes. Try to match other parts of the text. This hands-on experimentation is the key to building confidence.



Conclusion

Great work! You've successfully navigated your new workbench and conducted your first experiments. You now have the practical skills needed to test and understand any regular expression we build from here on out.

Key Points We've Covered:

  • It's best to treat regex as a distinct language and use a specialized tool (an IDE) like Regex101 to work with it.
  • The Regex101 interface consists of a test string panel, a regular expression panel, and an invaluable explanation panel that deciphers your patterns in real time.
  • Interactive testing provides immediate feedback, making it easy to see what works, what doesn't, and why.
  • Special characters (metacharacters) like . have unique meanings and must be escaped with a \ (e.g., \.) to be matched literally.

What's Next?

Now that you're comfortable in the lab, you're ready to start building. In our next lesson, we will begin with the most fundamental regex operation: writing a pattern to find a literal string (e.g., 'ABC-123') in a sample email. You'll see that you've already had a taste of it in today's experiments