Regular Expressions › What Are Regular Expressions?
Language AI HandbookAll chapters Regular Expressions › What Are Regular Expressions?One section · Sections in this chapter2 / 21 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
A regular expression (regex) is a sequence of characters that defines a search pattern. Think of it as a tiny programming language embedded within Python, specialized for matching and manipulating text. Regex is not unique to Python. The same core syntax works in Java and JavaScript. It also works in Ruby and Perl, in Go, and in dozens of other languages, as well as in command-line tools like grep, sed, and awk. Learning regex once gives you a superpower that transfers across your entire toolkit.
A regular expression is a formal language for describing patterns in strings. It uses special characters called metacharacters to represent character classes and repetition, together with position and grouping, allowing a single pattern to match many different strings.
Regular expressions were invented by mathematician Stephen Kleene in 1951 as a notation for describing what he called "regular events" in his algebraic theory of finite automata. Ken Thompson brought them into software engineering when he implemented them in the QED text editor in 1968, and later in the original Unix tools like ed and grep. The name "grep" itself stands for "global regular expression print." For decades, regex lived primarily in text editors and Unix shells. Perl popularized them as a first-class language feature in the 1980s and 1990s, and they eventually found their way into every major programming language. The patterns you write today trace a direct line back to Kleene's mathematical notation from more than seventy years ago.
The power of regex comes from its expressiveness. The pattern \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b looks intimidating, but it matches most email addresses in a single line. Without regex, you'd need dozens of lines of conditional logic to achieve the same result. Consider what that email pattern says: match a word boundary, then one or more characters from the set of letters, digits, dots, underscores, percent signs, plus signs, and hyphens; then a literal @; then one or more characters from letters, digits, dots, and hyphens; then a literal dot; then two or more letters; then another word boundary. Stated in English it takes a full sentence. In regex it takes 48 characters. Notice that the pattern also naturally rejects invalid emails like @missing.com (no local part) or nodomain@ (no domain) without any explicit rejection logic. The structure of the pattern itself defines what is and is not an email address.
Let's start with a simple example:
import re
# A simple pattern: find the word "cat"
text = "The cat sat on the mat. The catalog was nearby."
pattern = r"cat"
# Find all matches
matches = re.findall(pattern, text)Text: 'The cat sat on the mat. The catalog was nearby.' Pattern: 'cat' Matches: ['cat', 'cat'] Number of matches: 2
The pattern cat matches the literal characters c, a, t in sequence. It found two matches: "cat" as a standalone word and "cat" inside "catalog". Let's verify the match positions:
# Find match positions
for match in re.finditer(pattern, text):
start, end = match.span()
context = text[max(0, start - 5) : end + 5]Match positions: Position 4-7: 'cat' in '...The cat sat ...' Position 28-31: 'cat' in '... The catalog ...'
The regex found "cat" as a standalone word and "cat" inside "catalog". This illustrates a key point: by default, regex matches anywhere in the text, including inside other words. We'll learn how to match whole words only using word boundaries.