REGEX
MASTERY

// Match. Extract. Validate.

PATTERNS ARE EVERYWHERE.

Email addresses, phone numbers, URLs, log entries—all have patterns. Regex is the universal language for describing and matching those patterns.

MASTER THE PATTERN.

Once you understand regex, you'll find patterns everywhere. Validation, extraction, search and replace—regex makes it all effortless.

BEGIN YOUR JOURNEY

// Your Training Path

Click a lesson to begin

LESSON 01

Introduction to Regex

What are regular expressions?

Beginner
LESSON 02

Literal Characters

Match exact characters.

Beginner
LESSON 03

Metacharacters

. ^ $ * + ? { } [ ] \ | ( )

Beginner
LESSON 04

Character Classes

[abc], [a-z], [^abc], \d \w \s

Beginner
LESSON 05

Quantifiers

* + ? {n} {n,m}

Beginner
LESSON 06

Anchors

^ $ \b \B word boundaries.

Intermediate
LESSON 07

Groups

Capturing groups and backreferences.

Intermediate
LESSON 08

Alternation

OR matching with |.

Intermediate
LESSON 09

Escaping

Match special characters literally.

Intermediate
LESSON 10

Lookahead/Lookbehind

Zero-width assertions.

Advanced
LESSON 11

Common Patterns

Email, URL, IP, date validation.

Advanced
LESSON 12

Regex in Tools

grep, sed, awk, and more.

Advanced

// Lesson 01: Introduction to Regex

×

What are Regular Expressions?

Regular expressions (regex) are patterns used to match text. They're used in programming, command line tools, and text editors to find and manipulate text based on patterns.

Where Regex is Used

  • grep: Search files for patterns
  • sed: Find and replace text
  • awk: Extract and process text
  • Programming: Python, JavaScript, PHP, etc.
  • Editors: Vim, VSCode, Sublime

Regex Flavors

  • BRE: Basic Regular Expressions (basic grep)
  • ERE: Extended Regular Expressions (grep -E)
  • PCRE: Perl Compatible (most powerful)

Quiz

1. What does regex stand for?

Show Answers
  1. Regular Expression

// Lesson 02: Literal Characters

×

Match Exactly

Most characters in regex match themselves. If you write "hello", it matches the string "hello".

Examples

# grep for literal "error"
grep 'error' logfile.txt

# Matches "error", "Error", "ERROR"? Only if case-insensitive flag used
grep -i 'error' logfile.txt

# Match "127.0.0.1"
grep '127.0.0.1' /etc/hosts

Case Sensitivity

# Default is case-sensitive
grep 'Error' file.txt  # only matches "Error"

# Use -i for case-insensitive
grep -i 'error' file.txt  # matches "Error", "ERROR", "error"

Quiz

1. By default, is regex case-sensitive?

Show Answers
  1. Yes

// Lesson 03: Metacharacters

×

Special Characters

These characters have special meaning in regex:

.   Match any single character
^   Match at start of string
$   Match at end of string
*   Match 0 or more of preceding
+   Match 1 or more of preceding
?   Match 0 or 1 of preceding
{ } Match specific number
[ ] Match character class
\   Escape special character
|   Alternation (OR)
( ) Group
$1  Backreference

Examples

# . matches any character
grep 'c.t' file.txt  # matches "cat", "cot", "cut"

# * matches 0 or more
grep 'ab*c' file.txt  # matches "ac", "abc", "abbc"

# + matches 1 or more
grep 'ab+c' file.txt  # matches "abc", "abbc" but NOT "ac"

Quiz

1. What does . (dot) match in regex?

Show Answers
  1. Any single character

// Lesson 04: Character Classes

×

Character Class Syntax

[abc]    Match a, b, or c
[^abc]   NOT a, b, or c
[a-z]    Any lowercase letter
[A-Z]    Any uppercase letter
[0-9]    Any digit
[a-zA-Z] Any letter
[a-zA-Z0-9] Any alphanumeric

Shorthand Classes

\d   Digit [0-9]
\D   Non-digit [^0-9]
\w   Word character [a-zA-Z0-9_]
\W   Non-word character
\s   Whitespace (space, tab, newline)
\S   Non-whitespace

Examples

# Match any digit
grep '[0-9]' file.txt
grep '\d' file.txt

# Match not a digit
grep '[^0-9]' file.txt

# Match vowel
grep '[aeiou]' file.txt

# Match not a vowel
grep '[^aeiouAEIOU]' file.txt

Quiz

1. What does \d match?

Show Answers
  1. Any digit [0-9]

// Lesson 05: Quantifiers

×

Quantifier Syntax

*      0 or more
+      1 or more
?      0 or 1 (optional)
{n}    Exactly n times
{n,}   n or more times
{n,m}  Between n and m times

Examples

# Match US phone number
grep '[0-9]{3}-[0-9]{3}-[0-9]{4}' file.txt

# Match IP address (simplified)
grep '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' file.txt

# Match optional s
grep 'colors?' file.txt  # matches "color" and "colors"

# 1 or more digits
grep '[0-9]+' file.txt

# Whitespace
grep '\s+' file.txt

Greedy vs Non-Greedy

# Greedy (matches as much as possible)
grep '<.+>' file.txt  # matches entire 
...
# Non-greedy (matches as little as possible) grep '<.+?>' file.txt # matches
and
separately

Quiz

1. What does {3,5} mean?

Show Answers
  1. Between 3 and 5 times (inclusive)

// Lesson 06: Anchors

×

Anchors

Anchors don't match characters—they match positions.

^   Start of string (or line with -E)
$   End of string (or line with -E)
\b  Word boundary
\B  Non-word boundary

Examples

# Lines starting with "error"
grep '^error' logfile.txt

# Lines ending with "failed"
grep 'failed$' logfile.txt

# Entire line is "error"
grep '^error$' logfile.txt

# Word "the" (not in "there")
grep '\bthe\b' file.txt

Quiz

1. What does ^ match?

Show Answers
  1. Start of string (or line)

// Lesson 07: Groups

×

Capturing Groups

Parentheses () create groups that capture matched text.

Basic Groups

# Basic group
grep '(ab)+' file.txt  # matches "ab", "abab", "ababab"

# Capture group reference
# sed replacement: \1 refers to first group
sed 's/\(capture\) \1/\1/g' file.txt

Backreferences

# Find repeated words
grep '\b\([a-zA-Z]+\) \1\b' file.txt

# Replace with single word (sed)
sed 's/\([a-zA-Z]+\) \1/\1/g' file.txt

Named Groups

# PCRE (grep -P) named groups
grep -P '(?[a-zA-Z]+) \k' file.txt

Quiz

1. What do parentheses create in regex?

Show Answers
  1. Capturing groups

// Lesson 08: Alternation

×

The OR Operator

The | operator matches either the expression before or after it.

Examples

# Match "error" or "warning"
grep -E 'error|warning' logfile.txt

# Match "cat" or "dog" or "bird"
grep -E 'cat|dog|bird' file.txt

# With grouping
grep -E 'gr(e|a)y' file.txt  # matches "grey" or "gray"

Precedence

# Without parens (matches "abc" OR "xyz")
grep -E 'abc|xyz'

# With parens (matches "abc123" OR "xyz123")
grep -E '(abc|xyz)123'

Quiz

1. What does | match in regex?

Show Answers
  1. OR (either/or)

// Lesson 09: Escaping

×

Escaping Special Characters

Use backslash \ to match special characters literally.

Examples

# Match literal dot
grep 'example\.com' file.txt

# Match literal asterisk
grep '\*' file.txt

# Match literal dollar sign
grep '\$' file.txt

# Match literal backslash
grep '\\' file.txt

# Match [ literally
grep '\[' file.txt

When to Escape

# In grep, escape these: . ^ $ * + ? { } [ ] \ |
# With -E (extended), fewer escapes needed
grep -E '\.' file.txt  # still escape . in BRE

Quiz

1. How do you match a literal dot?

Show Answers
  1. \.

// Lesson 10: Lookahead/Lookbehind

×

Zero-Width Assertions

Lookahead and lookbehind check for patterns without consuming characters.

Syntax

(?=pattern)   Positive lookahead
(?!pattern)   Negative lookahead
(?<=pattern)  Positive lookbehind
(?

                

Examples

# Positive lookahead: password followed by =
grep -P 'password(?==)' file.txt

# Negative lookahead: not followed by =
grep -P 'password(?!=)' file.txt

# Positive lookbehind: preceded by Mr.
grep -P '(?<=Mr\.)\w+' file.txt

# Practical: extract domain from URLs (lookbehind)
grep -oP '(?<=@)[^@]+' file.txt  # email after @

Quiz

1. Do lookahead assertions consume characters?

Show Answers
  1. No (they're zero-width)

// Lesson 11: Common Patterns

×

Email Address

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

URL

https?://[^\s]+

IP Address (simplified)

[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}

Date Formats

# YYYY-MM-DD
[0-9]{4}-[0-9]{2}-[0-9]{2}

# MM/DD/YYYY
[0-9]{2}/[0-9]{2}/[0-9]{4}

# DD.MM.YYYY
[0-9]{2}\.[0-9]{2}\.[0-9]{4}

Phone Numbers

# US format
[0-9]{3}-[0-9]{3}-[0-9]{4}

# International
\+[0-9]{1,3}[0-9]{4,14}

Quiz

1. What is a common email regex pattern for the @ domain?

Show Answers
  1. @[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

// Lesson 12: Regex in Tools

×

grep

# Basic grep (BRE)
grep 'pattern' file.txt

# Extended grep (ERE) - less escaping needed
grep -E 'pattern+' file.txt

# Perl-compatible (most features)
grep -P 'pattern' file.txt

# Case insensitive
grep -i 'pattern' file.txt

# Invert match
grep -v 'pattern' file.txt

# Line numbers
grep -n 'pattern' file.txt

sed

# Find and replace
sed 's/old/new/g' file.txt

# Extended regex
sed -E 's/(capture) this/\1 that/' file.txt

awk

# Match pattern
awk '/pattern/ { print }' file.txt

# With condition on field
awk '$3 ~ /pattern/ { print }' file.txt

Congratulations!

You've mastered regex! You now understand:

  • Literal and metacharacters
  • Character classes [abc] and \d \w \s
  • Quantifiers * + ? {n,m}
  • Anchors ^ $ \b
  • Groups and backreferences
  • Alternation |
  • Escaping special characters
  • Lookahead/lookbehind
  • Common patterns for email, URL, IP

// Why Regex

Regex is the universal language for pattern matching. Once you understand it, you'll find patterns everywhere—in validation, extraction, search, and replace operations.

Master regex and you'll handle text processing tasks that would otherwise require complex programming.

Match. Extract. Validate.

// Tools & References

regex101

Online regex tester

regex101.com

QuickRef

Regex cheatsheet

QuickRef

RegexOne

Interactive tutorial

RegexOne

Regular-Expressions.info

Comprehensive guide

Regex Info