Skip to main content

Welcome to a Grasp lesson

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

Lesson 1

Introduction to Regular Expressions

Recommended resources

Hello! Welcome to your first lesson in our course on using regular expressions for your mail triage project.

I'm excited to guide you through this journey. Your background in philosophy and digital transformation gives you a unique advantage: you're comfortable with formal systems and understand the need for precise rules in automated processes. This is the very essence of regular expressions.

Introduction

Today's Goal: This first lesson is all about building a solid conceptual foundation. By the end, you will be able to define a regular expression as a formal language for specifying search patterns in text.

We won't be writing complex code today. Instead, we'll focus on understanding what a regular expression is, why it's called that, and the core idea behind how it works. Think of this as learning the fundamental principles of a new logical system before you start applying its rules.

Why it matters: Just as a legal contract needs precise clauses to be effective, an automated email sorting system needs precise rules to work correctly. Regular expressions are the language we use to write those rules.

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



1. What is a Regular Expression? A Formal Definition

At its heart, a regular expression (often shortened to "regex") is a tiny, highly specialized language. It's not a programming language like Python or Java, but rather a formal way to describe patterns in text.

To get a quick, clear definition, let's start with a very short video.

Activity 1: Watch a Quick Intro (30 seconds)

Please watch the first 26 seconds of the following video. It provides a concise and accurate definition that directly addresses our learning goal.

Regular Expressions Tutorial

As the video states, a regular expression is a "formal language for specifying search patterns in text." They are a fundamental tool in computer science, used for everything from the "find and replace" function in your word processor to validating the format of an email address on a web form.


2. Deconstructing the Name: "Regular Expression"

Your philosophy background will likely make you appreciate that the name itself carries specific meaning. Why "regular"? Why "expression"?

The term comes from a branch of theoretical computer science called formal language theory. Let's break it down.

Activity 2: Read the "Jargon Buster" (5 minutes)

The CS Field Guide offers a perfect explanation. Please read the short "Jargon Buster" section in the article below.

Formal Languages 15.4. Regular expressions

Focus on the section titled "Regular expression Jargon Buster". This will directly explain the two parts of the name.

Key Takeaways:

  • Expression: This is because a regex is an expression—a sequence of symbols that are combined according to a set of rules, much like a mathematical expression like (2 + 3) * 5.
  • Regular: This refers to the fact that these expressions describe a specific class of languages known as regular languages. This is a formal classification for sets of strings that can be recognized by a relatively simple computational model.

You don't need to dive deep into formal language theory, but it's useful to know that "regular" signifies that these patterns have a well-defined mathematical and logical foundation. It's this formal structure that makes them so powerful and predictable.


3. The Core Mechanism: Literals and Metacharacters

So, how does a regex actually describe a pattern? It does so by using a combination of two types of characters:

  1. Literal Characters: Most characters in a regex simply match themselves. The pattern cat will find the exact sequence of letters c-a-t in a text.
  2. Metacharacters: These are special characters that don't match themselves but act as commands. They might mean "match any digit," "match this or that," or "this part is optional."

This combination is what allows a short pattern to match a wide variety of strings.

Activity 3: Understand the Basic Concepts (10 minutes)

The Wikipedia article on regular expressions gives a clear overview of this core idea. Please read the "Definition" and "Basic concepts" sections.

Regular expression

Focus on these points:

  • The initial definition of a regex as a "sequence of characters that specifies a match pattern."
  • The example H(ä|ae?)ndel, which shows how metacharacters ((, |, ?, )) allow one pattern to match three different strings ("Handel", "Händel", and "Haendel"). This illustrates the idea of a regex defining a set of strings.

Think of it like a philosophical proposition. The regex /Error: [0-9]+/ doesn't just represent one string; it represents the entire set of strings that start with "Error: " followed by one or more digits. It defines a category.


4. Connecting to Your Goal: Mail Triage

Let's bring this back to your project. Your goal is to triage emails by identifying key information, like client reference numbers. These numbers rarely appear in the exact same format.

Consider these variations you might find in email subjects or bodies:

  • Ref: ABC-12345
  • Client ID: XYZ-987
  • Case #DEF456

A simple text search for "Ref:" would miss the other two. This is where regex comes in. You can construct a single pattern—a single formal expression—that defines what a "reference number" looks like in your context.

Your pattern might say something like:

"Find the word 'Ref', 'Client ID', or 'Case', followed by a colon or a hash symbol, followed by some combination of letters, dashes, and numbers."

This is the "search pattern" we're talking about. The rest of this course will be about learning the specific metacharacters and syntax needed to write that sentence in the formal language of regular expressions.



Conclusion

Excellent work. You've just grasped the foundational concept that underpins all of regular expressions.

Key Points We've Covered:

  • A regular expression is a sequence of characters that forms a search pattern.
  • It's considered a formal language, meaning it has precise rules and a basis in computer science theory.
  • The name signifies an expression that defines a regular language (a specific category of text patterns).
  • Regexes work by using literal characters (which match themselves) and metacharacters (which have special meanings) to define a set of strings to match.

What's Next?

Now that you understand what a regex is, it's time to start working with them. In our next lesson, we will cover the learning outcome: Use an interactive tool like Regex101 to test and debug patterns against sample text. We'll fire up a powerful online tool that will be our "laboratory" for the rest of the course, allowing us to build and test our patterns in real time.