Skip to main content

Welcome to a Grasp lesson

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

Lesson 3

Matching Literal Strings with Regex

Recommended resources

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

In our last lesson, you got acquainted with your "regex laboratory," Regex101. You saw how this interactive tool provides instant feedback, making it the perfect environment to build and test patterns. You even had a small preview of today's topic during your hands-on experimentation.

Introduction

Today's Goal: This lesson focuses on the most fundamental building block of any regular expression. By the end of our session, you will be able to write a pattern to find a literal string (e.g., 'ABC-123') in a sample email.

Why it matters: Your mail triage project will often require finding exact keywords, phrases, or identifiers—like "Invoice", "Urgent", or a specific client code. Literal string matching is the regex equivalent of a Ctrl+F search, and it's the foundation upon which all more complex patterns are built. Understanding this simple concept crisply is essential.

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



1. The Concept of a Literal Match

At its heart, a regular expression is a sequence of characters that defines a search pattern. The simplest pattern is one where each character is "literal."

A literal character is one that matches itself. The character a in a pattern matches the character a in the text. There is a one-to-one correspondence. You can think of this in philosophical terms: the character in the pattern is a constant that matches only its identical counterpart in the text, not a variable that can stand for other things.

When you string these literal characters together, you create a literal string pattern. The regex cat is simply an instruction to the regex engine: "find a c, immediately followed by an a, immediately followed by a t."

Activity 1: Understanding the Foundation (5 minutes)

To solidify this concept, please read the following brief sections from two excellent resources. They explain this core idea clearly and concisely.

  1. In Regex Tutorial - Literal Characters and Special Characters, read only the first section, "Literal Characters."
  2. In Literal Characters: Regular Expressions REGEX Explained, read the sections "Understanding Literal Characters" and "Combining Literal Characters."

These readings confirm that the most basic regex operation is matching an exact, case-sensitive sequence of characters.



2. Seeing it in Action

Reading about a concept is one thing; seeing it happen brings it to life. The following short video clip demonstrates this exact principle.

Activity 2: A Quick Visual (1 minute)

Watch this segment from Corey Schafer's popular regex tutorial. He demonstrates searching for the literal string ABC.

Regular Expressions (Regex) Tutorial: How to Match Any Pattern of Text

  • Watch from 01:41 to 02:23.
  • Notice how ABC matches the literal characters ABC in the text, but not BCA because the order is different, and not abc because of case-sensitivity.


3. Hands-On Practice: Finding Identifiers

Now it's time to apply this in your Regex101 lab. We'll use sample text that mimics the kind of data you'll find in emails for your triage project.

Activity 3: Writing Your First Patterns (15 minutes)

  1. Click this link to open a pre-configured Regex101 session: https://regex101.com/r/kYp2yV/1

    • The Test String pane contains several lines from different sample emails.
    • The Regular Expression pane is empty, waiting for your input.
  2. Your task is to write patterns to find the following literal strings. Type each pattern into the "Regular Expression" box and observe how Regex101 highlights the match in the text below.

    • Find a simple keyword: Type Invoice into the regex box. You'll see it immediately matches the word "Invoice" on line 4.

    • Find a client ID: Clear the box and type SRA-45-B. The pattern matches the exact client ID on line 2. This demonstrates that non-alphabetic characters like the hyphen (-) are, by default, treated as literals.

    • Test Case-Sensitivity: Now, clear the box and type invoice (all lowercase). Notice that there are no matches. As you've read, regex is case-sensitive by default. This is a critical feature, preventing accidental matches. (As you saw in the last lesson, you can use the i flag to make a pattern case-insensitive if needed).

    • Find the example from our learning outcome: Clear the box and type ABC-123. It correctly finds the Case ID on line 1.

Congratulations! You have successfully written patterns to find literal strings.



4. When a Character Isn't Literal: A Preview

You might be wondering, if a matches a and - matches -, are all characters literal?

Not quite. As you saw briefly in our last lesson with the period (.), some characters are reserved for special functions. These "metacharacters" are what give regular expressions their power, allowing them to match types of text (like "any number" or "any letter") rather than just literal strings.

We don't need to learn what they all do right now, but it's crucial to know they exist.

Activity 4: Recognizing Special Characters (2 minutes)

Please read the short "Special Characters" section of the first article we looked at.

Your goal here is not to memorize their functions, but simply to recognize that characters like . * + ( [ ^ and $ have special meanings. To match them literally, you must "escape" them with a backslash (\), as you did with \. in the previous lesson's experiment.



Conclusion

In this lesson, you've mastered the first and most fundamental principle of regular expressions.

Key Points We've Covered:

  • Literal Characters: Most characters (letters, numbers, many symbols) are "literal" and match themselves exactly.
  • Literal Strings: A sequence of literal characters in a pattern will match that exact sequence in the text.
  • Case-Sensitivity: By default, patterns are case-sensitive (A does not match a).
  • Metacharacters: A small set of characters are reserved for special functions, which gives regex its true power.

What's Next?

You now have a solid foundation. You know how to find an exact string, and you're aware that some characters do more. In our next lesson, we'll dive into our first metacharacter: you will learn how to use the dot (.) metacharacter to match any single character in a pattern. This will be your first step from matching what you already know to matching what you can describe.