Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 1: Introduction

What are regular expressions?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Regular expressions match and manipulate strings, which makes them powerful tools for searching, editing, and processing text. Many programming languages and tools support them, so they’re an essential skill for developers and data scientists.

History and evolution

The mathematician Stephen Cole Kleene introduced regular expressions in the 1950s to describe the algebraic properties of regular sets. Computer science later adopted them, and they’re now a basic part of text processing. Modern implementations have added a wide range of features and optimizations over the years.

Applications of regular expressions

Common uses include:

  • Text searching and matching: finding patterns in text, such as specific words or phrases.
  • Text editing and replacement: changing text based on patterns, such as replacing every instance of a word.
  • Input validation: checking that user input matches a format, such as an email address or a phone number.
  • Data extraction: pulling the information you need out of large text datasets, such as log files or web pages.
  • Syntax highlighting: highlighting code in text editors and IDEs according to the language’s rules.
  • Data transformation: converting data from one format to another with pattern-based rules.

Overview of regular expression syntax

A regular expression combines literal characters with special symbols. The basic pieces are:

  • Literal characters match themselves (a matches the character ‘a’).
  • Meta characters are special symbols that stand for classes of characters or control how matching works (. matches any single character except a newline).
  • Quantifiers say how many times an element should match (* matches zero or more occurrences).
  • Anchors mark positions in the text, such as the start or end of a line (^ matches the start of a line).
  • Character classes define a set of characters to match ([a-z] matches any lowercase letter).
  • Grouping and capturing let you treat several elements as one and keep the matched text for later processing ((abc) matches ‘abc’ and captures it).

The next chapters cover each of these in more detail.